Forum Discussion

khanh_tran's avatar
khanh_tran
New Contributor III
5 years ago
Solved

How to merge all objects in an array

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!

  • 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.

12 Replies

  • 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.

    • khanh_tran's avatar
      khanh_tran
      New Contributor III

      Woah. That’s great. I didn’t know that. You’re my hero. hahaha. Here is my result. I worked.

  • If you merge all objects into one, the values will be overwritten for the key with the same name in each object. Is this what you want ?

    • khanh_tran's avatar
      khanh_tran
      New Contributor III

      yes, sure. that’s all I want. Because every object will be not duplicated except the key-participant_survey_progress_sk. The field is also group by key, so it’s also not problem.

  • Hi @khanh_tran ,

    You can use JSON Splitter after this output and split on $group, this will give you only the objects inside the group array then you can use a Group by N snap to group all input data into a single array ( don’t forget to set the setting Group Size to 0 ).

    • khanh_tran's avatar
      khanh_tran
      New Contributor III

      Thank you! But your answer is not my expectation. I need object[0] merge to object[1] and to object[n].

  • 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.

    • khanh_tran's avatar
      khanh_tran
      New Contributor III

      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)

  • dd_snaplogic's avatar
    dd_snaplogic
    New Contributor II

    @j.angelevski Under the group object if I have same key, for example

    this example as you can see it has same key, so if i use extend function it will override the value with the same key

    Is it possible to keep all the values and generate the matching keys dynamically in the same object.

    example:

    Here is the example slp file
    Example Groupby Merge.slp (6.6 KB)

    • bojanvelevski's avatar
      bojanvelevski
      Valued Contributor

      Hi @dd_snaplogic,

      The following function will give you the result you need:

      $group.reduce((acc,curr)=> acc.extend(curr.filter((v1,k1)=> k1!='abc').mapKeys((v,k)=> k+curr.num)),{}).extend($groupBy)

      Keep in mind that this will work only if you group the data by 1 field. If you need it to be grouped on multiple fields, then the expression will have to be amended.

      Hope this helps,
      Bojan