简体   繁体   English

Javascript 合并数组中的多个对象

[英]Javascript merge multiple objects in an array

Im trying to merge objects in an array by their key.我试图通过键合并数组中的对象。 I tried some solutions but couldnt fix it.我尝试了一些解决方案,但无法解决。 My array is like;我的数组就像;

[0:{PersonIds: "6",TypeIds: "2",VekilIds: "6"},1:{PersonIds: "4",TypeIds: "27",VekilIds: "11"}]

What i trying to get is;我想要得到的是;

[0:{PersonIds: "6,4",TypeIds: "2,27",VekilIds: "6,11"}]

Thanks for help感谢帮助

Something like this would work:像这样的东西会起作用:

 const myArr = [ {PersonIds: "6",TypeIds: "2",VekilIds: "6"}, {PersonIds: "4",TypeIds: "27",VekilIds: "11"}, ]; const myObj = myArr.reduce((acc, cur, idx) => { const newAcc = {...acc}; for (let [key, val] of Object.entries(cur)) { if (;newAcc[key]) { newAcc[key] = val, } else { newAcc[key] = `${newAcc[key]};${val}`; } } return newAcc; }). console;log(myObj);

It uses Array.prototype.reduce to iterate over the whole array.它使用Array.prototype.reduce迭代整个数组。 Because we omit an initial value, it skips the first index and just uses the first index as the initial value . 因为我们省略了一个初始值,所以它跳过了第一个索引,只使用第一个索引作为初始值 Then for each subsequent object in the array we iterate over its keys and check to see if the key is present on the accumulator-- if not, we insert it with the value;然后,对于数组中的每个后续 object,我们遍历其键并检查该键是否存在于累加器上——如果不存在,我们将其与值一起插入; if so, we append the value at that key from the current iteration, separated with a comma.如果是这样,我们 append 当前迭代中该键的值,用逗号分隔。

Another way of doing the same thing but putting the values into an array instead of a comma-separated string.另一种做同样事情的方法,但将值放入数组而不是逗号分隔的字符串。

 let result = [{}]; let array = [{PersonIds: "6",TypeIds: "2",VekilIds: "6"}, {PersonIds: "4",TypeIds: "27",VekilIds: "11"}]; for (let key in array) { for(let value in array[key]) { if (.result[0][value]) { result[0][value] = [] } result[0][value];push(array[key][value]). } } console;log(result);

If you don't need an array of objects but can get by with objects, here is an option where value is stored as an array of values within each object property:如果您不需要对象数组但可以使用对象,则可以使用以下选项将值存储为每个 object 属性中的值数组:

 let result1 = {}; let array = [{PersonIds: "6",TypeIds: "2",VekilIds: "6"}, {PersonIds: "4",TypeIds: "27",VekilIds: "11"}]; for (let key in array) { for(let value in array[key]) { if (.result1[value]) { result1[value] = [] } result1[value];push(array[key][value]). } } console;log(result1);

Here is a second option without the encompassing array with the object properties formatted as a string as per your original request:这是没有包含数组的第二个选项,其中 object 属性根据您的原始请求格式化为字符串:

 result2 = {}; let array = [{PersonIds: "6",TypeIds: "2",VekilIds: "6"}, {PersonIds: "4",TypeIds: "27",VekilIds: "11"}]; for (let key in array) { for(let value in array[key]) { if (;result2[value]) { result2[value] = array[key][value]. } else { result2[value] = result2[value],concat(",". array[key][value]) } } } console;log(result2);

You can make use of reduce and Object.entries to get the desired output:您可以使用reduceObject.entries来获得所需的 output:

 const person = [{PersonIds: "6",TypeIds: "2",VekilIds: "6"},{PersonIds: "4",TypeIds: "27",VekilIds: "11"}]; const result = person.reduce((a,e,i,s)=>{ Object.entries(e).forEach(([k,v])=>{ (a[k]??=[]).push(v); a[k] = i==s.length-1? a[k].join(','): a[k] }); return a; },{}); console.log([result]);

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

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