簡體   English   中英

如何使用 Lodash 轉換數據

[英]How to transform data using Lodash

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

您需要_.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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM