【问题标题】:How to transform data using Lodash如何使用 Lodash 转换数据
【发布时间】:2019-12-10 19:42:05
【问题描述】:

我对 lodash 没有太多经验,我想转换我的数据。 我尝试使用 groupBy 和 map 但我无法获得预期的结果。 如果有人可以提供帮助将不胜感激。

样本数据

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>

预期结果

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
    }]
  }
]

预期结果 2

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

【问题讨论】:

    标签: javascript collections lodash


    【解决方案1】:

    您需要 _.groupBy()type,将项目映射到对象,并通过再次按 changeType 分组来获取 changeType 属性,映射要更新的项目并传播结果:

    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>

    要达到预期,您需要使用与 type 对象相同的逻辑:

    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>

    【讨论】:

    • 嗨 Ori 非常感谢您的回答,这是我所期待的,我也知道我需要更改什么才能将 changeType 1 或 2 移动到 type 下。现在你在扁平结构中
    • 我不确定你的意思。请更新预期结果
    • 我添加了预期结果 2,并接受了您的第一个答案。第二个结果将更好地阐明输出
    • 预期 2 不现实。您需要将数组分配给一个键,例如 changeTypes
    • 我还将删除每个对象的 updated 属性。查看预期的 2 个结果。
    猜你喜欢
    • 2015-10-12
    • 1970-01-01
    • 2014-10-23
    • 2016-04-22
    • 1970-01-01
    • 2019-05-22
    • 2019-01-26
    • 1970-01-01
    相关资源
    最近更新 更多