Search code examples
javascriptcollectionslodash

How to transform data using Lodash


I dont have much experince with lodash and I want to transform my data. I tried using groupBy and map but i am unable to get the expected result. Would greatly appreciate if someone could help.

Sample Data

var sample = [{
    "changeType": 1,
    "type": "changeAccount",
    "updated": {
      "id": 71,
      "company": 124201,
      "user": 8622
    }
  },
  {
    "changeType": 2,
    "type": "changeAccount",
    "updated": {
      "id": 70,
      "company": 124201,
      "user": 8622
    }
  },
  {
    "changeType": 1,
    "type": "changeproduct",
    "updated": {
      "id": 15,
      "company": 124201,
      "user": 8622
    }
  }
]

// what I tried
var result = _.mapValues(_.groupBy(sample, "type"), x => x.map(y => _.omit(y, "changeType")));

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

Expected Result

var output = [
  {
    "type": "changeAccount",
    1: [{
      "updated": 'for changeType 1 under type changeAccount
    }],
    2: [{
      "updated": for changeType 2 under type changeAccount
    }]
  },

  {
    "type": "changeProduct",
    1: [{
      "updated": for changeType 1 under type changeProduct
    }]
  }
]

Expected Result 2

const sample2 = [
  {type:"changeAccount", changeType: [{1:[{updated}],{2:[{updated}]}}] 
]

Solution

  • You'll need to _.groupBy() the type, map the items to objects, and get the changeType properties by grouping again by changeType, mapping the items to take just updated, and spreading the results:

    const { map, groupBy, mapValues, pick } = _
    
    const fn = arr =>
      map(groupBy(arr, 'type'), (group, type) => ({ // group and map to array of objects
        type,
        ...mapValues( // spread after mapping the values to extract updated
          groupBy(group, 'changeType'), // group again by changeType
          items => items.map(item => pick(item, 'updated') // extract the updated from each item
        ))
      }))
    
    const sample = [{"changeType":1,"type":"changeAccount","updated":{"id":71,"company":124201,"user":8622}},{"changeType":2,"type":"changeAccount","updated":{"id":70,"company":124201,"user":8622}},{"changeType":1,"type":"changeproduct","updated":{"id":15,"company":124201,"user":8622}}]
    
    const result = fn(sample)
    
    console.log(result)
    <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.js"></script>

    To get expected you'll need to use the same logic used for the type objects:

    const { map, groupBy, mapValues, pick } = _
    
    const fn = arr =>
      map(groupBy(arr, 'type'), (group, type) => ({ // group and map to array of objects
        type,
        changeTypes: map( // spread after mapping the values to extract updated
          groupBy(group, 'changeType'), // group again by changeType
          (items, changeType) => ({
            changeType,
            updated: items.map(item => item.updated) // extract the updated from each item
          })
        )
      }))
    
    const sample = [{"changeType":1,"type":"changeAccount","updated":{"id":71,"company":124201,"user":8622}},{"changeType":2,"type":"changeAccount","updated":{"id":70,"company":124201,"user":8622}},{"changeType":1,"type":"changeproduct","updated":{"id":15,"company":124201,"user":8622}}]
    
    const result = fn(sample)
    
    console.log(result)
    <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.js"></script>