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

Thank you @ddellsperger. The expression is not gross at all and it looks great. It looks like it could get me to the result but I honestly did not try it because I would prefer the keys to be in order since they have to be written as columns to an error report. I really appreciate your help though ๐Ÿ™‚

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),{} ))

vgautam64
New Contributor III

Thanks @Supratim. This expression gets me to the desired result as well but I realized I don't need the Snaplogic's ensureArray function.

bojanvelevski
Valued Contributor

Hey @vgautam64,

Try the following expression:

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

Regards,

Bojan

vgautam64
New Contributor III

Hi @bojanvelevski. This is such an elegant expression to achieve the task. I really appreciate the help. Thanks a lot!