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
-
1You're aware that the datatype as written is not valid JavaScript, right?Alexander Nied– Alexander Nied2021年05月07日 13:18:13 +00:00Commented May 7, 2021 at 13:18
-
It's like combine two string into one, not combining two objectsDhaval Darji– Dhaval Darji2021年05月07日 13:22:59 +00:00Commented May 7, 2021 at 13:22
-
Thats a data comes from remote. I just logged itGunsela– Gunsela2021年05月07日 13:27:00 +00:00Commented May 7, 2021 at 13:27
3 Answers 3
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.
-
Glad to help-- happy coding!Alexander Nied– Alexander Nied2021年05月07日 13:34:12 +00:00Commented May 7, 2021 at 13:34
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);
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]);