1

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

Gunsela
  • 360
  • 1
  • 7
  • 18

3 Answers3

1

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. 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; if so, we append the value at that key from the current iteration, separated with a comma.

Alexander Nied
  • 12,804
  • 4
  • 25
  • 45
1

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:

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:

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);
VikingGlen
  • 1,705
  • 18
  • 18
0

You can make use of reduce and Object.entries to get the desired 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]);
gorak
  • 5,233
  • 1
  • 7
  • 19