gpt4 book ai didi

javascript - 如何使用 Lodash 转换数据

转载 作者:行者123 更新时间:2023-12-02 22:19:36 25 4
gpt4 key购买 nike

我对 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()类型,将项目映射到对象,并通过分组获取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>

关于javascript - 如何使用 Lodash 转换数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59274276/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com