Forum Discussion

vgautam64's avatar
vgautam64
New Contributor III
2 years ago
Solved

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

I have data that looks like this: I want to achieve the following result: Please help!
  • Supratim's avatar
    2 years ago

    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),{} ))
  • bojanvelevski's avatar
    2 years ago

    Hey vgautam64,

    Try the following expression:

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

    Regards,

    Bojan