3 回答

TA貢獻1770條經(jīng)驗 獲得超3個贊
你想深入擴展
$.extend(true, {}, x, y);
請參閱jQuery.extend([ deep ],target,object1 [,objectN ])的文檔

TA貢獻1829條經(jīng)驗 獲得超13個贊
不依賴jQuery的簡單javascript函數(shù)將幫助您合并具有嵌套對象的兩個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;
}
添加回答
舉報