$.extend deep merged array problem

Merged array problem:

var a0 = {};

var a1 = {a: [1,2,3,4]};

var a2 = {a: [6,7]};

$.extend(true,a0,a1,a2);

The result is: {a:[6,7,3,4]}

And the result I expect is: {a:[6,7]}

Hope to cover the length of the array.

Although the above situation can be achieved by using $.extend(false,a0,a1,a2);, other object attributes cannot be merged if false is used.

My solution: Special treatment for array attributes, because there is only one attribute in a specific business object that is an array, so let's do it briefly for now.

$.extend(true,a0,a1,a2);
if(a2.a){
    a0.a = a2.a;
}

Merge object:

var a1={b:{c:1}};
var a2={b:{d:2}};
$.extend(a1,a2);
//结果a1值为{b:{d:2}}

$.extend(true,a1,a2);
//结果a1值为{b:{c:1,d:2}}

 

Guess you like

Origin blog.csdn.net/xiaozaq/article/details/101052068