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. 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.
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]);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.