07-26-2021 01:31 AM
I got an array named “group” includes object items as the below picture
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).
Thank you!
Solved! Go to Solution.
07-26-2021 04:10 AM
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.
07-26-2021 03:34 AM
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.
07-26-2021 04:01 AM
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)
07-26-2021 04:10 AM
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.
07-26-2021 04:20 AM
Woah. That’s great. I didn’t know that. You’re my hero. hahaha. Here is my result. I worked.
04-25-2023 05:00 AM
@j.angelevski Is there a way where i can keep all the values?