3 回答

TA贡献1770条经验 获得超3个赞
你想深入扩展
$.extend(true, {}, x, y);
请参阅jQuery.extend([ deep ],target,object1 [,objectN ])的文档

TA贡献1829条经验 获得超13个赞
不依赖jQuery的简单javascript函数将帮助您合并具有嵌套对象的两个JSON对象。
function mergeJSON(source1,source2){
/*
* Properties from the Souce1 object will be copied to Source2 Object.
* Note: This method will return a new merged object, Source1 and Source2 original values will not be replaced.
* */
var mergedJSON = Object.create(source2);// Copying Source2 to a new Object
for (var attrname in source1) {
if(mergedJSON.hasOwnProperty(attrname)) {
if ( source1[attrname]!=null && source1[attrname].constructor==Object ) {
/*
* Recursive call if the property is an object,
* Iterate the object and set all properties of the inner object.
*/
mergedJSON[attrname] = mergeJSON(source1[attrname], mergedJSON[attrname]);
}
} else {//else copy the property from source1
mergedJSON[attrname] = source1[attrname];
}
}
return mergedJSON;
}
添加回答
举报