[英]How to combine 2 objects with same properties-Javascript
I want to be able to merge two JavaScript objects with same properties/keys defined. 我希望能够合并具有相同属性/键的两个JavaScript对象。 Just that the values are not duplicate. 只是这些值不是重复的。 For example I'd like to: 例如,我想:
var obj1 = {
0:{id: 33, name: 'abc', date: "12/12/12", type:"11" },
1: {id: 33, name: 'abc3', date: "12/13/12", type:"131"}
}
var obj2 = {
0:{id: 22, name: 'abc1', date: "12/1/13", type: "33" }
1:{id: 4, name: 'abc4', date: "12/4/12", type:"14"}
}
here is the fiddle: http://jsfiddle.net/q9c8k2yh/ this is the expected structure: 这是小提琴: http : //jsfiddle.net/q9c8k2yh/这是预期的结构:
I want the o/p to be: 我希望o / p为:
var obj3 = {
0: { id: 33, name: 'abc', date: "12/12/12", type:"11" },
1: { id: 22, name: 'abc1', date: "12/1/13", type: "33" }
}
I looked up at the sources online and the most common answer I get: 我在网上查找了来源,并得到了最常见的答案:
Object.assign(obj2, obj1);
I have tried: 我努力了:
obj3 = Object.assign(obj2, obj1);//returns { id: 33, name: 'abc', date: "12/12/12", type:"11" }
obj3 = Object.assign(obj1, obj2);//return { id: 22, name: 'abc1', date: "12/1/13", type: "33" }
obj3 = Object.assign({},obj2, obj1); //return { id: 33, name: 'abc', date: "12/12/12", type:"11" }
but none of them gives me both the objects. 但它们都不给我两个对象。 I do not want to replace one over the other. 我不想替换一个。 is there a way to achieve this? 有没有办法做到这一点?
Thanks! 谢谢!
What you are describing looks more like an array than an object. 您所描述的看起来更像是数组,而不是对象。 But if you want an Object with numeric keys, well you can do that: 但是,如果您想要一个带有数字键的对象,那么您可以这样做:
var obj1 = { id: 33, name: 'abc', date: "12/12/12", type:"11" } var obj2 = { id: 22, name: 'abc1', date: "12/1/13", type: "33" } let arr = [obj1, obj2] // turn array into object: let obj_all = {...arr} console.log(obj_all)
The new object will hold references to the old objects. 新对象将包含对旧对象的引用 。 It's not clear if that's what you are after, or if you want clones. 目前尚不清楚这是您要追求的,还是想要克隆。
To make clones you could of course spread the objects: 要制作克隆,您当然可以传播对象:
var obj1 = { id: 33, name: 'abc', date: "12/12/12", type:"11" } var obj2 = { id: 22, name: 'abc1', date: "12/1/13", type: "33" } let arr = [{...obj1}, {...obj2}] let obj_all = {...arr} // changes in original objects have no effect on clones obj1.id="New Value" console.log(obj_all)
Sounds like you're looking for a hash map instead of merging 2 objects: 听起来您正在寻找哈希图,而不是合并2个对象:
var obj3 = {
0: obj1,
1: obj2
}
Object.assign merges on top the prior argument, where the first argument is the object to replace, so: Object.assign在先前参数的顶部合并,其中第一个参数是要替换的对象,因此:
Object.assign(obj2, obj1);
Replaces obj2
with itself and obj1
shallowly merged over it. 用自身替换obj2
,然后将obj1
浅层合并。
You can clone them easily using object rest spread (ES 2018): 您可以使用对象剩余传播(ES 2018)轻松克隆它们:
var obj1 = { id: 33, name: 'abc', date: "12/12/12", type:"11" } var obj2 = { id: 22, name: 'abc1', date: "12/1/13", type: "33" } var obj3 = { 0: { ...obj1 }, 1: { ...obj2 } } console.log(obj3);
Now if you change obj1
it won't change obj3["0"]
. 现在,如果您更改obj1
,则不会更改obj3["0"]
。
Seems like you want to create a new object having integer values as keys and value as requested object 似乎您想创建一个以整数值作为键并以值作为请求对象的新对象
var arrayOfObjects = [obj1, obj2]; //put here object you want to 'merge'
var result = Object.assign({}, arrayOfObjects);
Or in case you need a function. 或者,如果您需要一个功能。 It'll look like: 它看起来像:
function merge(arrayOfObjects){
return object.assign({}, arrayOfObjects);
}
Another approach might be this. 另一种方法可能是这样。 Stringifies the item and parses it so to make deep clone. 对项目进行字符串化并对其进行解析,以进行深度克隆。
var obj1 = { id: 33, name: 'abc', date: "12/12/12", type:"11" } var obj2 = { id: 22, name: 'abc1', date: "12/1/13", type: { id: 3 } } const mergeWithReference = (...args) => { return args.reduce((acc,item,index) => { acc[index] = JSON.parse(JSON.stringify(item)) return acc }, {}) } const mergedObj = mergeWithReference(obj1, obj2) obj1.id = 85 obj2.type.id = 22 console.log(mergedObj);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.