使用条件将属性从一个对象复制到另一个对象

时间:2021-07-09 18:54:13

Lazy-me is wondering if there a better way to copy the properties in one object (source) over to another object (destination) only if the properties exist in the latter? It does not necessarily have to be using Underscore.

Lazy-me想知道是否有更好的方法将一个对象(源)中的属性复制到另一个对象(目标),只有后者存在属性?它不一定必须使用Underscore。

For example,

_.mixin({
    assign: function (o, destination, source) {
        for (var property in source) {
            if (destination.hasOwnProperty(property)) {
                destination[property] = source[property];
            }
        }
        return destination;
    }
});

console.log( _().assign({ a: 1, b: 2, d: 3 }, { a: 4, c: 5 }) ) // a: 4, b: 2, d: 3

2 个解决方案

#1


3  

One lazy option is:

一个懒惰的选择是:

_.extend(a, _.pick(b, _.keys(a)));

_.pick filters the source object by using the .keys of the destination object and the result is used for extending the destination object.

_.pick使用目标对象的.keys过滤源对象,结果用于扩展目标对象。

If you don't want to modify the original objects just pass an empty object to the _.extend function.

如果您不想修改原始对象,只需将空对象传递给_.extend函数。

_.extend({}, a, _.pick(b, _.keys(a)));

#2


4  

Use Object.assign(obj1, obj2); (if the properties exist in the latter) which is native in ES6 (no underscore.js is required).

使用Object.assign(obj1,obj2); (如果属性存在于后者中),这在ES6中是原生的(不需要下划线.js)。

The Object.assign() method is used to copy the values of all enumerable own properties from one or more source objects to a target object. It will return the target object. More info here.

Object.assign()方法用于将所有可枚举的自有属性的值从一个或多个源对象复制到目标对象。它将返回目标对象。更多信息在这里。

Example:

var o1 = { a: 1 };
var o2 = { b: 2 };
var o3 = { c: 3 };

var obj = Object.assign(o1, o2, o3);
console.log(obj);

Alternatively use undescore.js

或者使用unexcore.js

_.extend(destination, *sources)

or

_.extendOwn(destination, *sources)

Detailated information can be found here: http://underscorejs.org/#extend

有关详细信息,请访问:http://underscorejs.org/#extend

#1


3  

One lazy option is:

一个懒惰的选择是:

_.extend(a, _.pick(b, _.keys(a)));

_.pick filters the source object by using the .keys of the destination object and the result is used for extending the destination object.

_.pick使用目标对象的.keys过滤源对象,结果用于扩展目标对象。

If you don't want to modify the original objects just pass an empty object to the _.extend function.

如果您不想修改原始对象,只需将空对象传递给_.extend函数。

_.extend({}, a, _.pick(b, _.keys(a)));

#2


4  

Use Object.assign(obj1, obj2); (if the properties exist in the latter) which is native in ES6 (no underscore.js is required).

使用Object.assign(obj1,obj2); (如果属性存在于后者中),这在ES6中是原生的(不需要下划线.js)。

The Object.assign() method is used to copy the values of all enumerable own properties from one or more source objects to a target object. It will return the target object. More info here.

Object.assign()方法用于将所有可枚举的自有属性的值从一个或多个源对象复制到目标对象。它将返回目标对象。更多信息在这里。

Example:

var o1 = { a: 1 };
var o2 = { b: 2 };
var o3 = { c: 3 };

var obj = Object.assign(o1, o2, o3);
console.log(obj);

Alternatively use undescore.js

或者使用unexcore.js

_.extend(destination, *sources)

or

_.extendOwn(destination, *sources)

Detailated information can be found here: http://underscorejs.org/#extend

有关详细信息,请访问:http://underscorejs.org/#extend