用一个对象的属性替换一个对象的属性?

时间:2021-10-18 20:12:57

First I am not saying deep cloning one object to another.

首先,我并不是说将一个对象深深地克隆到另一个对象。

I have one object:

我有一个对象:

var data1 = {
               p1: values1,
               p2: values2,
               .....
            };

value1 and value2 can be any data type, like object or array.

value1和value2可以是任何数据类型,如对象或数组。

I have another object:

我有另一个对象:

var data2 = {
               p3: values3,
               p4: values4,
               .....
            };

value3 and value4 can be any data type, like object or array.

value3和value4可以是任何数据类型,如对象或数组。

Now I want to clone all data2's properties to data1.

现在我想将所有data2的属性克隆到data1。

The reason I want this is data1 is referenced, after data1's properties are changed I don't want to breake the reference.

我想要这个的原因是data1被引用,在data1的属性改变后我不想破坏引用。

If I do something like

如果我做的事情

 data1 = clone(data2)

I will break the reference.

我会打破参考。

Update: What means by breaking the references:

更新:通过打破引用意味着什么:

var data1 = {
             p1: value1
          }; 
var data2 = {
              p2: vale2
          };
var ref = data1;

If I do:

如果我做:

data1 = clone(data2);  

Now ref still point to the old data1, its value still be:

现在ref仍然指向旧的data1,它的值仍然是:

{
   p1: value1
}

But I want ref be data2

但我希望ref是data2

3 个解决方案

#1


4  

You could use this function:

你可以使用这个功能:

function replaceProperties(dest, src)  {
    for (var i in dest) 
        delete dest[i];
    for (var i in src) 
        dest[i] = src[i];
}

The code is obvious and seems like it does what you want. It doesn't "break reference" as you say it.

代码很明显,看起来就像你想要的那样。它并没有像你说的那样“打破参考”。

Though it might not do what you want when properties are not enumerable or from prototypes...

虽然当属性不可枚举或原型时它可能不会做你想要的......

If you don't have to support IE10 and before, using __proto__ or setPrototypeof, you can have your dest update when src properties are changed:

如果您不必支持IE10,之前使用__proto__或setPrototypeof,则可以在更改src属性时进行dest更新:

function replaceProperties(dest, src)  {
    for (var i in dest) 
        delete dest[i];
    Object.setPrototypeOf(dest, src);
}

#2


1  

Try:

尝试:

Object.extend(data1, data2);

Object.extend(data1,data2);

#3


-1  

Using jQuery.extend()

使用jQuery.extend()

for the deep copy: var object = $.extend({}, object1, object2);

对于深层副本:var object = $ .extend({},object1,object2);

more information http://api.jquery.com/jQuery.extend/#jQuery-extend-target-object1-objectN

更多信息http://api.jquery.com/jQuery.extend/#jQuery-extend-target-object1-objectN

#1


4  

You could use this function:

你可以使用这个功能:

function replaceProperties(dest, src)  {
    for (var i in dest) 
        delete dest[i];
    for (var i in src) 
        dest[i] = src[i];
}

The code is obvious and seems like it does what you want. It doesn't "break reference" as you say it.

代码很明显,看起来就像你想要的那样。它并没有像你说的那样“打破参考”。

Though it might not do what you want when properties are not enumerable or from prototypes...

虽然当属性不可枚举或原型时它可能不会做你想要的......

If you don't have to support IE10 and before, using __proto__ or setPrototypeof, you can have your dest update when src properties are changed:

如果您不必支持IE10,之前使用__proto__或setPrototypeof,则可以在更改src属性时进行dest更新:

function replaceProperties(dest, src)  {
    for (var i in dest) 
        delete dest[i];
    Object.setPrototypeOf(dest, src);
}

#2


1  

Try:

尝试:

Object.extend(data1, data2);

Object.extend(data1,data2);

#3


-1  

Using jQuery.extend()

使用jQuery.extend()

for the deep copy: var object = $.extend({}, object1, object2);

对于深层副本:var object = $ .extend({},object1,object2);

more information http://api.jquery.com/jQuery.extend/#jQuery-extend-target-object1-objectN

更多信息http://api.jquery.com/jQuery.extend/#jQuery-extend-target-object1-objectN