在JavaScript中合并两个关联数组的最佳/标准方式是什么?每个人都只是通过滚动自己的for
循环来做到这一点吗?
使用jQuery,您可以调用 $.extend
var obj1 = {a: 1, b: 2};
var obj2 = {a: 4, c: 110};
var obj3 = $.extend(obj1, obj2);
obj1 == obj3 == {a: 4, b: 2, c: 110} // Pseudo JS
(关联数组是js中的对象)
看这里:http : //api.jquery.com/jQuery.extend/
编辑:像rymo建议的那样,最好这样做:
obj3 = $.extend({}, obj1, obj2);
obj3 == {a: 4, b: 2, c: 110}
此处obj1(和obj2)保持不变。
edit2:在2018年,方法是通过Object.assign
:
var obj3 = Object.assign({}, obj1, obj2);
obj3 === {a: 4, b: 2, c: 110} // Pseudo JS
如果使用ES6,可以使用Spread运算符实现:
const obj3 = { ...obj1, ...obj2 };
现在在2016年,我会说最好/标准的方法是Object.assign()
Pure Javascript。不需要jQuery。
obj1 = {a: 1, b: 2};
obj2 = {a: 4, c: 110};
obj3 = Object.assign({},obj1, obj2); // Object {a: 4, b: 2, c: 110}
此处提供了更多信息,示例和polyfill:https :
//developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/assign
这是原型的工作方式:
Object.extend = function(destination, source) {
for (var property in source) {
if (source.hasOwnProperty(property)) {
destination[property] = source[property];
}
}
return destination;
};
称为,例如:
var arr1 = { robert: "bobby", john: "jack" };
var arr2 = { elizabeth: "liz", jennifer: "jen" };
var shortnames = Object.extend(arr1,arr2);
编辑:添加了hasOwnProperty()检查,如bucabay在注释中正确指出的
把事情简单化...
function mergeArray(array1,array2) {
for(item in array1) {
array2[item] = array1[item];
}
return array2;
}
下划线还具有扩展方法:
将源对象中的所有属性复制到目标对象。它是有序的,因此最后一个源将覆盖先前参数中的同名属性。
_.extend(destination, *sources)
_.extend({name : 'moe'}, {age : 50});
=> {name : 'moe', age : 50}
在dojo中,“合并”的2个对象/数组将是lang.mixin(destination, source)
-您也可以将多个源混合到一个目标中,等等-有关详细信息,请参见mixin函数的参考。
如果名称相同但值不同,是否要覆盖属性?
您是否要永久更改其中一个原始对象,
还是要返回新的合并对象?
function mergedObject(obj1, obj2, force){
for(var p in obj1) this[p]= obj1[p];
for(var p in obj2){
if(obj2.hasOwnProperty(p)){
if(force || this[p]=== undefined) this[p]= obj2[p];
else{
n= 2;
while(this[p+n]!== undefined)++n;
this[p+n]= obj2[p];
}
}
}
}
滚动自己的扩展/混合功能
function extend(objects) {
var args
, first = Array.prototype.slice.call(arguments, 0, 1)[0]
, second;
if (arguments.length > 1) {
second = Array.prototype.splice.call(arguments, 1, 1)[0];
for (var key in second) {
first[key] = second[key];
}
args = Array.prototype.slice.call(arguments, 0);
return extend.apply(this, args);
}
return first;
}
...
var briansDirections = {
step1: 'Remove pastry from wrapper.',
step2: 'Place pastry toaster.',
step3: 'Remove pastry from toaster and enjoy.',
};
extend(briansDirections, { step1: 'Toast Poptarts' }, { step2: 'Go ahead, toast \'em' }, { step3: 'Hey, are you sill reading this???' });
...
这只是延伸的图示的对象,递归。另外,请注意,此递归函数是TCO(优化尾调用),因为它的返回是对自身的最后一次调用。
此外,您可能需要有针对性的属性。在这种情况下,你可能要凝聚基于对象id
,quantity
或其他财产。这种方法可能有一本关于它的小书,并且需要对象并置,并且可能变得非常复杂。我已经为此编写了一个小型库,可根据要求提供。
希望这可以帮助!
- 在Javascript中,没有关联数组的概念,有对象
- 合并两个对象的唯一方法是循环其属性,并将指针复制到不是原始类型的值,并将原始类型的值复制到另一个实例
在2019年,您有2个不错的选择:
对象分配[ doc ]
const result = Object.assign({}, baseObject, updatingObject);
对象传播[ doc ]
const result = { ...baseObject, ...updatingObject};
第一个趋向于更安全,更标准和多价。这里的优点和缺点
Yahoo UI(YUI)也为此提供了一个辅助功能:
http://developer.yahoo.com/yui/examples/yahoo/yahoo_merge.html
YAHOO.namespace('example');
YAHOO.example.set1 = { foo : "foo" };
YAHOO.example.set2 = { foo : "BAR", bar : "bar" };
YAHOO.example.set3 = { foo : "FOO", baz : "BAZ" };
var Ye = YAHOO.example;
var merged = YAHOO.lang.merge(Ye.set1, Ye.set2, Ye.set3);
jQuery具有用于深度复制的布尔值。您可以这样做:
MergeRecursive = function(arr1, arr2){
$.extend(true, arr1, arr2);
return arr1;
};
您也可以编辑此函数以支持n数组合并。
ArrayMergeRecursive = function(){
if(arguments.length < 2){
throw new Error("ArrayMergeRecursive: Please enter two or more objects to merge!");
}
var arr1=arguments[0];
for(var i=0; i<=arguments.length; i++ ){
$.extend(true, arr1, arguments[i]);
}
return arr1;
};
所以现在你可以做
var arr1 = {'color': {'mycolor': 'red'}, 3: 5},
arr2 = {4: 10, 'color': {'favorite': 'green', 0: 'blue'}},
arr3 = ['Peter','Jhon','Demosthenes'],
results = ArrayMergeRecursive(arr1, arr2, arr3); // (arr1, arr2 ... arrN)
console.log("Result is:", results);
我需要一个深层对象合并。因此,所有其他答案对我没有太大帮助。_.extend和jQuery.extend效果很好,除非您像我一样有一个递归数组。但这还不错,您可以在五分钟内对其进行编程:
var deep_merge = function (arr1, arr2) {
jQuery.each(arr2, function (index, element) {
if (typeof arr1[index] === "object" && typeof element === "object") {
arr1[index] = deep_merge(arr1[index], element);
} else if (typeof arr1[index] === "array" && typeof element === "array") {
arr1[index] = arr1[index].concat(element);
} else {
arr1[index] = element;
}
});
return arr1;
}
要在jQuery中合并数组,$。merge怎么办?
var merged = $.merge([{id:3, value:'foo3'}], [{id:1, value:'foo1'}, {id:2, value:'foo2'}]);
merged[0].id == 3;
merged[0].value == 'foo3';
merged[1].id == 1;
merged[1].value == 'foo1';
merged[2].id == 2;
merged[2].value == 'foo2';
递归解决方案(也扩展对象的数组)+空检查
var addProps = function (original, props) {
if(!props) {
return original;
}
if (Array.isArray(original)) {
original.map(function (e) {
return addProps(e, props)
});
return original;
}
if (!original) {
original = {};
}
for (var property in props) {
if (props.hasOwnProperty(property)) {
original[property] = props[property];
}
}
return original;
};
测验
console.log(addProps([{a: 2}, {z: 'ciao'}], {timestamp: 13}));
console.log(addProps({single: true}, {timestamp: 13}));
console.log(addProps({}, {timestamp: 13}));
console.log(addProps(null, {timestamp: 13}));
[ { a: 2, timestamp: 13 }, { z: 'ciao', timestamp: 13 } ]
{ single: true, timestamp: 13 }
{ timestamp: 13 }
{ timestamp: 13 }
这是最好的解决方案。
obj1.unshift.apply( obj1, obj2 );
而且,obj1
可以在循环内增长而没有任何问题。(可以说obj2
是动态的)
文章标签:javascript , object
版权声明:本文为原创文章,版权归 javascript 所有,欢迎分享本文,转载请保留出处!
评论已关闭!