6 回答
TA贡献1842条经验 获得超12个赞
试试这个:var obj = $.extend(true, a, b);
语法:jQuery.extend( [deep ], target, object1 [, objectN ] )
深浅拷贝对应的参数就是[deep],是可选的,为true或false。默认情况是false(浅拷贝),并且false是不能够显示的写出来的。如果想写,只能写true(深拷贝)
TA贡献1776条经验 获得超12个赞
原生js 写的
function merge (target) {
for (let i = 1, j = arguments.length; i < j; i++) {
let source = arguments[i] || {};
for (let prop in source) {
if (source.hasOwnProperty(prop)) {
let value = source[prop];
if(typeof value === "object" ){
target[prop] = merge(target[prop]||{},value)
}else {
if (value !== undefined) {
target[prop] = value;
}
}
}
}
}
return target;
};
merge(a,b)
TA贡献1998条经验 获得超6个赞
var a = {
title: {
text: '饼图',
textStyle: {
color: '#ffffff',
fontSize: 45,
fontWeight: 'normal',
fontFamily: '华文细黑'
},
x: 'center',
y: 'center'
}
}
var b = {
title: {
text: 'PIE',
textStyle: {
color: '#cccccc'
}
},
test: 'test'
}
function merge(target, source) {
for (key in source) {
if (target.hasOwnProperty(key) && typeof target[key] === 'object') {
merge(target[key], source[key])
} else {
target[key] = source[key]
}
}
}
merge(a, b)
console.log(a)
添加回答
举报