gpt4 book ai didi

javascript - jQuery extent JSON数组参数替换

转载 作者:行者123 更新时间:2023-11-29 19:13:41 24 4
gpt4 key购买 nike

我正在尝试了解 jquery 扩展方法,根据 API documentation

The merge performed by $.extend() is not recursive by default; if a property of the first object is itself an object or array, it will be completely overwritten by a property with the same key in the second or subsequent object. The values are not merged.

它说数组或对象属性将不会合并并将被替换。在下面的示例中,参数是 JSON 数组,它正在合并而不是被替换。

var a = {
external : true,
parameter : [{name:'ip1'},{name:'ip2'},{name:'ip3'}]

};
var b = {
data:'Sumeet',
parameter : [{name:'ip1'},{name:'ip2'}]
};


$.extend(true,a,b);

console.log(a);

Output :


{
data: "Sumeet",
external: true,
parameter: [{
name: "ip1"
}, {
name: "ip2"
}, {
name: "ip3"
}]
}

参数应该只有ip1和ip2

最佳答案

请注意,您的引用说“默认”。后面的文档还说:

However, by passing true for the first function argument, objects will be recursively merged.

全段:

The merge performed by $.extend() is not recursive by default; if a property of the first object is itself an object or array, it will be completely overwritten by a property with the same key in the second or subsequent object. The values are not merged. This can be seen in the example below by examining the value of banana. However, by passing true for the first function argument, objects will be recursively merged.

关于javascript - jQuery extent JSON数组参数替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36889080/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com