简体   繁体   English

将对象数组作为Node.js中Array对象的值

[英]Making an array of object a value of a object in Array in Node.js

I have two arrays of objects. 我有两个对象数组。 example

 data1 = [{
     foo: '2',
     box: 'yes',
     id: '1234'
 }, {
     foo: '34',
     box: 'no',
     id: '1235'
 }];

 data2 = [{
     key: 'value',
     key: 'value',
     id: '1234'
 }, {
     key: 'value',
     key: 'value',
     id: '1235'
 }];

I need it like this based on matching id value : each obj has a unique key id, that much the second array. 我需要它基于匹配的id值:每个obj都有一个唯一的密钥id,就是第二个数组。

finalData = [{
    foo: '2',
    box: 'yes',
    id: '1234',
    key: 'value',
    key: 'value'
}, {
    box: '34',
    box: 'no',
    id: '1235',
    key: 'value',
    key: 'value'
}];

basically merging the two arrays of objects based on key id . 基本上根据键id合并两个对象数组。

Use Object.assign for this so that you can combine the array with the common id value, use find having condition that compares the id of both the objects. 为此使用Object.assign ,以便您可以将数组与公共id值组合,使用find having condition来比较两个对象的id

 var data1 =[ {foo:'2',box:'yes',id:'1234'},{foo:'34',box:'no',id:'1235'} ]; var data2 =[{key:'value',key2:'value2', id:'1234'},{key:'value',key2:'value2', id:'1235'}]; data1.map(x => Object.assign(x, data2.find(y => y.id == x.id))); console.log(data1); 

Since you have same key name multiple times which is ignored by the JSON object so I have taken a new property name key2 for this example. 由于您具有多次相同的键名,JSON对象会忽略该名称,因此我为此示例采用了新的属性名称key2

building on @Ankit answer, you can use the spread operator too !! 建立在@Ankit的答案上,你也可以使用传播运营商! something like this 这样的事情

 var data1 =[ {foo:'2',box:'yes',id:'1234'},{foo:'34',box:'no',id:'1235'} ]; var data2 =[{key:'value',key2:'value2', id:'1234'},{key:'value',key2:'value2', id:'1235'}]; const arr = data1.map(x => ({ ...x, ...data2.find(y => y.id == x.id)})); console.log(arr); 

You can also use reduce() , find() of array and Object.assign() to get required result. 您还可以使用reduce()find()Object.assign()来获取所需的结果。

DEMO DEMO

 const data1 = [{foo: '2',box: 'yes',id: '1234'}, {foo: '34',box: 'no',id: '1235'}], data2 = [{key: 'value',key1: 'value',id: '1234'}, {key: 'value',key1: 'value',id: '1235'}]; let result = data1.reduce((r,{id,...rest})=>{ let find=data2.find(o=>o.id==id); if(find){ r.push(Object.assign(rest,find)); } return r; },[]) console.log(result) 
 .as-console-wrapper {max-height: 100% !important;top: 0;} 

Here is another alternative to achieve this: 以下是实现此目的的另一种方法:

  • Iterate over first array and create a new Map object where ids will be used as keys and relevant object as its value. 迭代第一个数组并创建一个新的Map对象,其中id将用作键,相关对象用作其值。
  • Iterate over second array and update corresponding objects with same ids using Object.assign() in Map object. 迭代第二个数组并使用Map对象中的Object.assign()更新具有相同ID的相应对象。
  • Return an array of updated objects in Map object. 返回Map对象中的更新对象数组。

Demo: 演示:

 let data1 = [{foo: '2', box: 'yes', id: '1234'}, {foo: '34', box: 'no', id: '1235'}], data2 = [{key1: 'value', key2: 'value', id: '1234'}, {key1: 'value', key2: 'value', id: '1235'}]; let merge = (d1, d2, m = new Map(d1.map(o => [o.id, o]))) => ( d2.forEach(o => m.set(o.id, Object.assign(m.get(o.id), o))), [...m.values()] ); console.log(merge(data1, data2)); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

Docs: 文档:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM