cancel
Showing results for 
Search instead for 
Did you mean: 

How to merge all objects in an array

khanh_tran
New Contributor II

I got an array named “group” includes object items as the below picture
Screen Shot 2021-07-26 at 15.22.51
How can I merge all object items to one object? My expected result as the below picture. But, imagine that the number of item is unknown. It can be 3,4,5 , … (n).
Screen Shot 2021-07-26 at 15.24.27

Thank you!

1 ACCEPTED SOLUTION

j_angelevski
Contributor III

That won’t be a problem here, because I’m using destructuring assignment ( the three dots ) before the $group array ...$group, this will always work for every $group array regardless of the array size, you don’t have to get each index from the $group array, just use ...$group, this will destructure the array and will be the same as writing $group[0]... $group[n] multiple times.

View solution in original post

12 REPLIES 12

j_angelevski
Contributor III

Just use the following expression, this should do the job:

{}.extend(...$group)

This will simply put all objects from the array into a single object and it will overwrite duplicate keys.

Thank you. But my problem here is “I don’t know how many item in $group array”. I cannot type {}.extend($group[0],$group[1],$group[n…]) (T_T)

j_angelevski
Contributor III

That won’t be a problem here, because I’m using destructuring assignment ( the three dots ) before the $group array ...$group, this will always work for every $group array regardless of the array size, you don’t have to get each index from the $group array, just use ...$group, this will destructure the array and will be the same as writing $group[0]... $group[n] multiple times.

Woah. That’s great. I didn’t know that. You’re my hero. hahaha. Here is my result. I worked.
Screen Shot 2021-07-26 at 18.19.08

dd_snaplogic
New Contributor II

@j.angelevski Is there a way where i can keep all the values?