CocoHot CocoHot - 2 months ago 22
JSON Question

Merge array of complex objects by using Lodash

I'm new to Lodash and trying to solve this problem but could find a good way to do it.

I have an array of objects return back from the database. The data is structured as below:

var data = [{
index: 1,
Aoo: "a1",
Boo: 'b2',
}, {
index: 1,
Aoo: "a2",
Boo: 'b2',
}, {
index: 2,
Aoo: "a3",
Boo: 'b3',
}];


I want to first group the objects by index, then group the attribute "Aoo" and "Boo" and put it in an array attribute called
param
.

var result = [{
index: 1,
param: [{
Aoo: 'A1',
Boo: 'B1'
},{
Aoo: 'A2',
Boo: 'B2',
}]
}, {
index: 2,
param: [{
Aoo: 'A3',
Boo: 'B3'
}]
}
]


I can do it manually but I want to make the most use of Lodash's functionality. Right now I only know I can achieve the first step of grouping by using
_.groupBy('index')
and I am stuck at what to do next.

Answer

You are almost there. Here is how this can be done using lodash

var data = [{
    index: 1,
    Aoo: "a1",
    Boo: 'b2',
}, {
    index: 1,
    Aoo: "a2",
    Boo: 'b2',
}, {
    index: 2,
    Aoo: "a3",
    Boo: 'b3',
}];

var result = _.chain(data)
  .groupBy('index')
  .map((value, key) => {
    return {
      index: Number(key), //the index was transformed into a string, this will make it a number again.
      param: _.map(value, o => _.omit(o, 'index'))//do not include the index key from the previous objects
    }
  })
  .value();

console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.16.1/lodash.min.js"></script>

The second part is, admittedly, slightly trickier than the grouping, but not by much. Since indexing gives you this structure:

{
  "1": [
    {
      "index": 1,
      "Aoo": "a1",
      "Boo": "b2"
    },
    {
      "index": 1,
      "Aoo": "a2",
      "Boo": "b2"
    }
  ],
  "2": [
    {
      "index": 2,
      "Aoo": "a3",
      "Boo": "b3"
    }
  ]
}

All you really want to do is go over it, make each key be the index property and then you are left with an array of the inital objects. Each needs to be transformed by removing the index key and that will be assigned to the param value. This is done in one go using .map. Unfortunately, it's not as pretty as it could look but I think that's the best you can do with the base version of Lodash.