我需要能够在运行时合并两个(非常简单的)JavaScript对象.例如,我想:
var obj1 = { food: 'pizza', car: 'ford' } var obj2 = { animal: 'dog' } obj1.merge(obj2); //obj1 now has three properties: food, car, and animal
有没有人有这个脚本或知道内置的方法来做到这一点?我不需要递归,我不需要合并函数,只需要平面对象上的方法.
ECMAScript 2018标准方法
你会使用对象传播:
let merged = {...obj1, ...obj2}; /** There's no limit to the number of objects you can merge. * Later properties overwrite earlier properties with the same name. */ const allRules = {...obj1, ...obj2, ...obj3};
ECMAScript 2015(ES6)标准方法
/* For the case in question, you would do: */ Object.assign(obj1, obj2); /** There's no limit to the number of objects you can merge. * All objects get merged into the first object. * Only the object in the first argument is mutated and returned. * Later properties overwrite earlier properties with the same name. */ const allRules = Object.assign({}, obj1, obj2, obj3, etc);
(参见MDN JavaScript参考)
ES5和早期的方法
for (var attrname in obj2) { obj1[attrname] = obj2[attrname]; }
请注意,这会简单地添加的所有属性obj2
,以obj1
这可能不是你想要什么,如果你仍然想使用未修改obj1
.
如果你使用的是一个遍布原型的框架,那么你必须得到更好的支票hasOwnProperty
,但是这些代码适用于99%的情况.
功能示例:
/** * Overwrites obj1's values with obj2's and adds obj2's if non existent in obj1 * @param obj1 * @param obj2 * @returns obj3 a new object based on obj1 and obj2 */ function merge_options(obj1,obj2){ var obj3 = {}; for (var attrname in obj1) { obj3[attrname] = obj1[attrname]; } for (var attrname in obj2) { obj3[attrname] = obj2[attrname]; } return obj3; }
jQuery还有一个实用工具:http://api.jquery.com/jQuery.extend/.
取自jQuery文档:
// Merge options object into settings object var settings = { validate: false, limit: 5, name: "foo" }; var options = { validate: true, name: "bar" }; jQuery.extend(settings, options); // Now the content of settings object is the following: // { validate: true, limit: 5, name: "bar" }
上面的代码将改变名为的现有对象settings
.
如果要在不修改任何参数的情况下创建新对象,请使用以下命令:
var defaults = { validate: false, limit: 5, name: "foo" }; var options = { validate: true, name: "bar" }; /* Merge defaults and options, without modifying defaults */ var settings = $.extend({}, defaults, options); // The content of settings variable is now the following: // {validate: true, limit: 5, name: "bar"} // The 'defaults' and 'options' variables remained the same.
在和谐的ECMAScript 2015年(ES6)规定Object.assign
,将做到这一点.
Object.assign(obj1, obj2);
当前的浏览器支持正在变得越来越好,但如果您正在为没有支持的浏览器进行开发,则可以使用polyfill.
我用google搜索代码来合并对象属性,最后来到这里.但是由于没有任何递归合并代码,我自己写了.(也许jQuery扩展是递归BTW?)无论如何,希望其他人也会觉得它很有用.
(现在代码不使用Object.prototype
:)
/* * Recursively merge properties of two objects */ function MergeRecursive(obj1, obj2) { for (var p in obj2) { try { // Property in destination object set; update its value. if ( obj2[p].constructor==Object ) { obj1[p] = MergeRecursive(obj1[p], obj2[p]); } else { obj1[p] = obj2[p]; } } catch(e) { // Property in destination object not set; create it and set its value. obj1[p] = obj2[p]; } } return obj1; }
o1 = { a : 1, b : 2, c : { ca : 1, cb : 2, cc : { cca : 100, ccb : 200 } } }; o2 = { a : 10, c : { ca : 10, cb : 20, cc : { cca : 101, ccb : 202 } } }; o3 = MergeRecursive(o1, o2);
生成对象o3之类的
o3 = { a : 10, b : 2, c : { ca : 10, cb : 20, cc : { cca : 101, ccb : 202 } } };
请注意,underscore.js
' extend
-method在单行中执行此操作:
_.extend({name : 'moe'}, {age : 50}); => {name : 'moe', age : 50}
与jQuery extend()类似,您在AngularJS中具有相同的功能:
// Merge the 'options' object into the 'settings' object var settings = {validate: false, limit: 5, name: "foo"}; var options = {validate: true, name: "bar"}; angular.extend(settings, options);
我今天需要合并对象,这个问题(和答案)对我帮助很大.我尝试了一些答案,但没有一个符合我的需求,所以我结合了一些答案,自己添加了一些东西,并提出了一个新的合并功能.这里是:
var merge = function() {
var obj = {},
i = 0,
il = arguments.length,
key;
for (; i < il; i++) {
for (key in arguments[i]) {
if (arguments[i].hasOwnProperty(key)) {
obj[key] = arguments[i][key];
}
}
}
return obj;
};
一些示例用法:
var t1 = {
key1: 1,
key2: "test",
key3: [5, 2, 76, 21]
};
var t2 = {
key1: {
ik1: "hello",
ik2: "world",
ik3: 3
}
};
var t3 = {
key2: 3,
key3: {
t1: 1,
t2: 2,
t3: {
a1: 1,
a2: 3,
a4: [21, 3, 42, "asd"]
}
}
};
console.log(merge(t1, t2));
console.log(merge(t1, t3));
console.log(merge(t2, t3));
console.log(merge(t1, t2, t3));
console.log(merge({}, t1, { key1: 1 }));
您可以使用对象传播属性 - 目前是第3阶段ECMAScript提议.
const obj1 = { food: 'pizza', car: 'ford' };
const obj2 = { animal: 'dog' };
const obj3 = { ...obj1, ...obj2 };
console.log(obj3);
给出的解决方案应进行修改,以检查source.hasOwnProperty(property)
在for..in
分配前环-否则,你最终会复制整个原型链,这是很少所需的属性...
一种Object.assign
方法是ECMAScript 2015(ES6)标准的一部分,完全符合您的需求.(IE
不支持)
var clone = Object.assign({}, obj);
Object.assign()方法用于将所有可枚举自身属性的值从一个或多个源对象复制到目标对象.
阅读更多...
该填充工具以支持旧的浏览器:
if (!Object.assign) { Object.defineProperty(Object, 'assign', { enumerable: false, configurable: true, writable: true, value: function(target) { 'use strict'; if (target === undefined || target === null) { throw new TypeError('Cannot convert first argument to object'); } var to = Object(target); for (var i = 1; i < arguments.length; i++) { var nextSource = arguments[i]; if (nextSource === undefined || nextSource === null) { continue; } nextSource = Object(nextSource); var keysArray = Object.keys(nextSource); for (var nextIndex = 0, len = keysArray.length; nextIndex < len; nextIndex++) { var nextKey = keysArray[nextIndex]; var desc = Object.getOwnPropertyDescriptor(nextSource, nextKey); if (desc !== undefined && desc.enumerable) { to[nextKey] = nextSource[nextKey]; } } } return to; } }); }
以下两个可能是一个很好的起点.lodash还有针对这些特殊需求的定制功能!
_.extend
(http://underscorejs.org/#extend)
_.merge
(https://lodash.com/docs#merge)
顺便说一下,你们所做的就是覆盖属性,而不是合并......
这就是JavaScript对象区域真正合并的方式:只有to
对象中不是对象本身的键才会被覆盖from
.其他一切都将真正融合.当然,你可以改变这种行为,不要覆盖任何存在的东西,只有to[n] is undefined
等等......:
var realMerge = function (to, from) { for (n in from) { if (typeof to[n] != 'object') { to[n] = from[n]; } else if (typeof from[n] == 'object') { to[n] = realMerge(to[n], from[n]); } } return to; };
用法:
var merged = realMerge(obj1, obj2);
这是我的刺伤
支持深度合并
不改变参数
需要任意数量的参数
不扩展对象原型
不依赖于另一个库(jQuery,MooTools,Underscore.js等)
包括检查hasOwnProperty
是短 :)
/* Recursively merge properties and return new object obj1 <- obj2 [ <- ... ] */ function merge () { var dst = {} ,src ,p ,args = [].splice.call(arguments, 0) ; while (args.length > 0) { src = args.splice(0, 1)[0]; if (toString.call(src) == '[object Object]') { for (p in src) { if (src.hasOwnProperty(p)) { if (toString.call(src[p]) == '[object Object]') { dst[p] = merge(dst[p] || {}, src[p]); } else { dst[p] = src[p]; } } } } } return dst; }
例:
a = { "p1": "p1a", "p2": [ "a", "b", "c" ], "p3": true, "p5": null, "p6": { "p61": "p61a", "p62": "p62a", "p63": [ "aa", "bb", "cc" ], "p64": { "p641": "p641a" } } }; b = { "p1": "p1b", "p2": [ "d", "e", "f" ], "p3": false, "p4": true, "p6": { "p61": "p61b", "p64": { "p642": "p642b" } } }; c = { "p1": "p1c", "p3": null, "p6": { "p62": "p62c", "p64": { "p643": "p641c" } } }; d = merge(a, b, c); /* d = { "p1": "p1c", "p2": [ "d", "e", "f" ], "p3": null, "p5": null, "p6": { "p61": "p61b", "p62": "p62c", "p63": [ "aa", "bb", "cc" ], "p64": { "p641": "p641a", "p642": "p642b", "p643": "p641c" } }, "p4": true }; */
Object.assign()
ECMAScript 2015(ES6)
这是一项新技术,是ECMAScript 2015(ES6)标准的一部分.该技术的规范已经完成,但请查看兼容性表,了解各种浏览器的使用情况和实现状态.
Object.assign()方法用于将所有可枚举自身属性的值从一个或多个源对象复制到目标对象.它将返回目标对象.
var o1 = { a: 1 }; var o2 = { b: 2 }; var o3 = { c: 3 }; var obj = Object.assign(o1, o2, o3); console.log(obj); // { a: 1, b: 2, c: 3 } console.log(o1); // { a: 1, b: 2, c: 3 }, target object itself is changed.
对于不太复杂的对象,您可以使用JSON:
var obj1 = { food: 'pizza', car: 'ford' } var obj2 = { animal: 'dog', car: 'chevy'} var objMerge; objMerge = JSON.stringify(obj1) + JSON.stringify(obj2); // {"food": "pizza","car":"ford"}{"animal":"dog","car":"chevy"} objMerge = objMerge.replace(/\}\{/, ","); // \_ replace with comma for valid JSON objMerge = JSON.parse(objMerge); // { food: 'pizza', animal: 'dog', car: 'chevy'} // Of same keys in both objects, the last object's value is retained_/
请注意,在此示例中,"} {" 不得出现在字符串中!
执行此操作的最佳方法是使用Object.defineProperty添加不可枚举的适当属性.
这样,如果要使用Object.prototype.extend创建属性,您仍然可以迭代对象属性,而无需新创建的"扩展".
希望这有助于:
Object.defineProperty(Object.prototype, "extend", { enumerable: false, value: function(from) { var props = Object.getOwnPropertyNames(from); var dest = this; props.forEach(function(name) { if (name in dest) { var destination = Object.getOwnPropertyDescriptor(from, name); Object.defineProperty(dest, name, destination); } }); return this; } });
一旦你有了这个工作,你可以做:
var obj = { name: 'stack', finish: 'overflow' } var replacement = { name: 'stock' }; obj.extend(replacement);
我刚刚在这里写了一篇关于它的博客文章:http://onemoredigit.com/post/1527191998/extending-objects-in-node-js
有一个叫库deepmerge
在GitHub上:这似乎是得到一些牵引.它是独立的,可通过npm和bower包管理器获得.
我倾向于使用或改进这个而不是从答案中复制粘贴代码.
你可以简单地使用jQuery extend
var obj1 = { val1: false, limit: 5, name: "foo" }; var obj2 = { val2: true, name: "bar" }; jQuery.extend(obj1, obj2);
现在obj1
包含的所有值obj1
和obj2
原型有这个:
Object.extend = function(destination,source) { for (var property in source) destination[property] = source[property]; return destination; }
obj1.extend(obj2)
会做你想做的.
只要有人使用Google Closure Library:
goog.require('goog.object'); var a = {'a': 1, 'b': 2}; var b = {'b': 3, 'c': 4}; goog.object.extend(a, b); // Now object a == {'a': 1, 'b': 3, 'c': 4};
数组存在类似的辅助函数:
var a = [1, 2]; var b = [3, 4]; goog.array.extend(a, b); // Extends array 'a' goog.array.concat(a, b); // Returns concatenation of array 'a' and 'b'
合并对象很简单.
var obj1 = { food: 'pizza', car: 'ford' }
var obj2 = { animal: 'dog', car: 'BMW' }
var obj3 = {a: "A"}
var mergedObj = Object.assign(obj1,obj2,obj3)
// or using the Spread operator (...)
var mergedObj = {...obj1,...obj2,...obj3}
console.log(mergedObj);
在MooTools中,有Object.merge():
Object.merge(obj1, obj2);
我扩展了David Coallier的方法:
添加了合并多个对象的可能性
支持深层对象
覆盖参数(如果最后一个参数是布尔值,则检测到该参数)
如果override为false,则不会覆盖任何属性,但会添加新属性.
用法:obj.merge(merges ... [,override]);
这是我的代码:
Object.defineProperty(Object.prototype, "merge", { enumerable: false, value: function () { var override = true, dest = this, len = arguments.length, props, merge, i, from; if (typeof(arguments[arguments.length - 1]) === "boolean") { override = arguments[arguments.length - 1]; len = arguments.length - 1; } for (i = 0; i < len; i++) { from = arguments[i]; if (from != null) { Object.getOwnPropertyNames(from).forEach(function (name) { var descriptor; // nesting if ((typeof(dest[name]) === "object" || typeof(dest[name]) === "undefined") && typeof(from[name]) === "object") { // ensure proper types (Array rsp Object) if (typeof(dest[name]) === "undefined") { dest[name] = Array.isArray(from[name]) ? [] : {}; } if (override) { if (!Array.isArray(dest[name]) && Array.isArray(from[name])) { dest[name] = []; } else if (Array.isArray(dest[name]) && !Array.isArray(from[name])) { dest[name] = {}; } } dest[name].merge(from[name], override); } // flat properties else if ((name in dest && override) || !(name in dest)) { descriptor = Object.getOwnPropertyDescriptor(from, name); if (descriptor.configurable) { Object.defineProperty(dest, name, descriptor); } } }); } } return this; } });
示例和测试用例:
function clone (obj) { return JSON.parse(JSON.stringify(obj)); } var obj = { name : "trick", value : "value" }; var mergeObj = { name : "truck", value2 : "value2" }; var mergeObj2 = { name : "track", value : "mergeObj2", value2 : "value2-mergeObj2", value3 : "value3" }; assertTrue("Standard", clone(obj).merge(mergeObj).equals({ name : "truck", value : "value", value2 : "value2" })); assertTrue("Standard no Override", clone(obj).merge(mergeObj, false).equals({ name : "trick", value : "value", value2 : "value2" })); assertTrue("Multiple", clone(obj).merge(mergeObj, mergeObj2).equals({ name : "track", value : "mergeObj2", value2 : "value2-mergeObj2", value3 : "value3" })); assertTrue("Multiple no Override", clone(obj).merge(mergeObj, mergeObj2, false).equals({ name : "trick", value : "value", value2 : "value2", value3 : "value3" })); var deep = { first : { name : "trick", val : "value" }, second : { foo : "bar" } }; var deepMerge = { first : { name : "track", anotherVal : "wohoo" }, second : { foo : "baz", bar : "bam" }, v : "on first layer" }; assertTrue("Deep merges", clone(deep).merge(deepMerge).equals({ first : { name : "track", val : "value", anotherVal : "wohoo" }, second : { foo : "baz", bar : "bam" }, v : "on first layer" })); assertTrue("Deep merges no override", clone(deep).merge(deepMerge, false).equals({ first : { name : "trick", val : "value", anotherVal : "wohoo" }, second : { foo : "bar", bar : "bam" }, v : "on first layer" })); var obj1 = {a: 1, b: "hello"}; obj1.merge({c: 3}); assertTrue(obj1.equals({a: 1, b: "hello", c: 3})); obj1.merge({a: 2, b: "mom", d: "new property"}, false); assertTrue(obj1.equals({a: 1, b: "hello", c: 3, d: "new property"})); var obj2 = {}; obj2.merge({a: 1}, {b: 2}, {a: 3}); assertTrue(obj2.equals({a: 3, b: 2})); var a = []; var b = [1, [2, 3], 4]; a.merge(b); assertEquals(1, a[0]); assertEquals([2, 3], a[1]); assertEquals(4, a[2]); var o1 = {}; var o2 = {a: 1, b: {c: 2}}; var o3 = {d: 3}; o1.merge(o2, o3); assertTrue(o1.equals({a: 1, b: {c: 2}, d: 3})); o1.b.c = 99; assertTrue(o2.equals({a: 1, b: {c: 2}})); // checking types with arrays and objects var bo; a = []; bo = [1, {0:2, 1:3}, 4]; b = [1, [2, 3], 4]; a.merge(b); assertTrue("Array stays Array?", Array.isArray(a[1])); a = []; a.merge(bo); assertTrue("Object stays Object?", !Array.isArray(a[1])); a = []; a.merge(b); a.merge(bo); assertTrue("Object overrides Array", !Array.isArray(a[1])); a = []; a.merge(b); a.merge(bo, false); assertTrue("Object does not override Array", Array.isArray(a[1])); a = []; a.merge(bo); a.merge(b); assertTrue("Array overrides Object", Array.isArray(a[1])); a = []; a.merge(bo); a.merge(b, false); assertTrue("Array does not override Object", !Array.isArray(a[1]));
我的equals方法可以在这里找到:JavaScript中的对象比较
在Ext JS 4中,可以按如下方式完成:
var mergedObject = Ext.Object.merge(object1, object2) // Or shorter: var mergedObject2 = Ext.merge(object1, object2)
请参阅merge(object):Object.
哇..这是我见过多个页面的第一篇StackOverflow帖子.抱歉添加另一个"答案"
这种方法适用于ES5和早期 - 还有很多其他解决ES6的问题.
我没有看到任何"深层"对象合并使用该arguments
属性.这是我的答案 - 紧凑和递归,允许传递无限的对象参数:
function extend() { for (var o = {}, i = 0; i < arguments.length; i++) { // if (arguments[i].constructor !== Object) continue; for (var k in arguments[i]) { if (arguments[i].hasOwnProperty(k)) { o[k] = arguments[i][k].constructor === Object ? extend(o[k] || {}, arguments[i][k]) : arguments[i][k]; } } } return o; }
注释掉的部分是可选的..它将简单地跳过不是对象传递的参数(防止错误).
例:
extend({ api: 1, params: { query: 'hello' } }, { params: { query: 'there' } }); // outputs {api: 1, params: {query: 'there'}}
这个答案现在只是海洋中的一滴......
var obj1 = { food: 'pizza', car: 'ford' } var obj2 = { animal: 'dog' } // result result: {food: "pizza", car: "ford", animal: "dog"}
使用jQuery.extend() - 链接
// Merge obj1 & obj2 to result var result1 = $.extend( {}, obj1, obj2 );
使用_.merge() - 链接
// Merge obj1 & obj2 to result var result2 = _.merge( {}, obj1, obj2 );
使用_.extend() - 链接
// Merge obj1 & obj2 to result var result3 = _.extend( {}, obj1, obj2 );
使用Object.assign()ECMAScript 2015(ES6) - 链接
// Merge obj1 & obj2 to result var result4 = Object.assign( {}, obj1, obj2 );
全部输出
obj1: { animal: 'dog' } obj2: { food: 'pizza', car: 'ford' } result1: {food: "pizza", car: "ford", animal: "dog"} result2: {food: "pizza", car: "ford", animal: "dog"} result3: {food: "pizza", car: "ford", animal: "dog"} result4: {food: "pizza", car: "ford", animal: "dog"}
根据Markus'和vsync'的回答,这是一个扩展版本.该函数接受任意数量的参数.它可用于在DOM节点上设置属性并生成值的深层副本.但是,第一个参数是通过引用给出的.
要检测DOM节点,使用isDOMNode()函数(请参阅Stack Overflow问题JavaScript isDOM - 如何检查JavaScript对象是否是DOM对象?)
它在Opera 11,Firefox 6,Internet Explorer 8和Google Chrome 16中进行了测试.
function mergeRecursive() { // _mergeRecursive does the actual job with two arguments. var _mergeRecursive = function (dst, src) { if (isDOMNode(src) || typeof src !== 'object' || src === null) { return dst; } for (var p in src) { if (!src.hasOwnProperty(p)) continue; if (src[p] === undefined) continue; if ( typeof src[p] !== 'object' || src[p] === null) { dst[p] = src[p]; } else if (typeof dst[p]!=='object' || dst[p] === null) { dst[p] = _mergeRecursive(src[p].constructor===Array ? [] : {}, src[p]); } else { _mergeRecursive(dst[p], src[p]); } } return dst; } // Loop through arguments and merge them into the first argument. var out = arguments[0]; if (typeof out !== 'object' || out === null) return out; for (var i = 1, il = arguments.length; i < il; i++) { _mergeRecursive(out, arguments[i]); } return out; }
设置HTML元素的innerHTML和样式
mergeRecursive( document.getElementById('mydiv'), {style: {border: '5px solid green', color: 'red'}}, {innerHTML: 'Hello world!'});
合并数组和对象.请注意,undefined可用于保留左侧数组/对象中的值.
o = mergeRecursive({a:'a'}, [1,2,3], [undefined, null, [30,31]], {a:undefined, b:'b'}); // o = {0:1, 1:null, 2:[30,31], a:'a', b:'b'}
任何不属于JavaScript对象的参数(包括null)都将被忽略.除第一个参数外,DOM节点也被丢弃.请注意,即像新String()一样创建的字符串实际上是对象.
o = mergeRecursive({a:'a'}, 1, true, null, undefined, [1,2,3], 'bc', new String('de')); // o = {0:'d', 1:'e', 2:3, a:'a'}
如果要将两个对象合并为一个新对象(不影响其中任何一个),请将{}作为第一个参数
var a={}, b={b:'abc'}, c={c:'cde'}, o; o = mergeRecursive(a, b, c); // o===a is true, o===b is false, o===c is false
编辑(由ReaperSoon提供):
还要合并数组
function mergeRecursive(obj1, obj2) { if (Array.isArray(obj2)) { return obj1.concat(obj2); } for (var p in obj2) { try { // Property in destination object set; update its value. if ( obj2[p].constructor==Object ) { obj1[p] = mergeRecursive(obj1[p], obj2[p]); } else if (Array.isArray(obj2[p])) { obj1[p] = obj1[p].concat(obj2[p]); } else { obj1[p] = obj2[p]; } } catch(e) { // Property in destination object not set; create it and set its value. obj1[p] = obj2[p]; } } return obj1; }
let obj1 = {a:1, b:2};
let obj2 = {c:3, d:4};
let merged = {...obj1, ...obj2};
console.log(merged);
使用Underscore.js,合并一个对象数组:
var arrayOfObjects = [ {a:1}, {b:2, c:3}, {d:4} ]; _(arrayOfObjects).reduce(function(memo, o) { return _(memo).extend(o); });
它导致:
Object {a: 1, b: 2, c: 3, d: 4}
您应该使用lodash的默认值
_.defaultsDeep({ 'user': { 'name': 'barney' } }, { 'user': { 'name': 'fred', 'age': 36 } }); // ? { 'user': { 'name': 'barney', 'age': 36 } }