cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

Unable to merge the objects of an array as a single object

vgautam64
New Contributor III

I have data that looks like this:

vgautam64_0-1698338938548.png

I want to achieve the following result:

vgautam64_1-1698338972225.png

Please help!

2 ACCEPTED SOLUTIONS

Supratim
Contributor III

Hi @vgautam64 , I am unable to attached pipeline. so give you an example- 

Data: 

{
    "carList": [
    {
    "color""purple",
    "type""minivan",
    "registration""2017-01-03",
    "capacity"7
  },
  {
    "color""red",
    "type""station wagon",
    "registration""2018-01-03",
    "capacity"5
  }, {
    "color""black",
    "type""toyota",
    "registration""2017-01-03",
    "capacity"5
  },
  {
    "color""blue",
    "type""Maruti",
    "registration""2018-01-03",
    "capacity"5
  }
]
}
1. use mapper for adding index : $carList.map((val,index)=> val.mapKeys((v,k)=> k+(index+1)))    : $carList (target path of mapper)
2. use another mapper to merge in single array obejct : sl.ensureArray($carList.reduce((curr,acc)=>curr.extend(acc),{} ))

View solution in original post

bojanvelevski
Valued Contributor

Hey @vgautam64,

Try the following expression:

$group.reduce((acc,curr,ind)=> acc.extend(curr.mapKeys((val,key)=>key+ind)),{})

Regards,

Bojan

View solution in original post

9 REPLIES 9

vgautam64
New Contributor III

@bojanvelevski @dmiller @tstack can you help?

vgautam64
New Contributor III

Note that this is just an example and the number of objects in the group array is not fixed.

dmiller
Admin Admin
Admin

Thanks for bumping this up. I haven't had a chance to take a look into it yet, but let me see what I can do or who I can find to help (unfortunately, @tstack is no longer around).


Diane Miller
Community Manager

ddellsperger
Employee
Employee

This is a pretty gross expression, but you could put this in a mapper to achieve the result, since the output is an object rather than array, the order likely doesn't really matter in this case:

$group.reduce((acc, val, ind) => acc.extend(...val.entries().map((k) => {[k[0] + (ind+1)]: k[1]})), {})