gpt4 book ai didi

javascript - lodash 从对象数组中查找公共(public)值

转载 作者:行者123 更新时间:2023-11-30 14:10:30 24 4
gpt4 key购买 nike

从对象数组中找到共同的值并将它们横梁

尝试使用 lodash groupBy

var data =[
{
"dc": 1,
"effDate": "1/2/2019",
"expDate": "1/2/2019",
"rate": 1,
"minCharge": 2
},
{
"dc": 1,
"effDate": "1/2/2019",
"expDate": "1/2/2019",
"rate": 2,
"minCharge": 6
},
{
"dc": 1,
"effDate": "1/2/2019",
"expDate": "1/2/2019",
"rate": 4,
"minCharge": 7
}
]

var expectedResult=[
{
"dc": 1,
"effDate": "1/2/2019",
"expDate": "1/2/2019",
"rateCharge": [
{
"rate": 1,
"minCharge": 2
},
{
"rate": 2,
"minCharge": 6
},
{
"rate": 4,
"minCharge": 7
}
]
}
]

在数据 dc 中,effDate、expDate 是相同的,所以我需要将常见的东西保持为平面结构,并将重复的项目移到 rateCharge 中。

var expectedResult=uniqBy(data,(val1.rate,val2.rate) => {
val1.rate!=val2.rate;
});

我已经尝试使用 lodash uniqBy 属性,但我没有得到预期的结果。

最佳答案

您需要按 effDate 对项目进行分组,然后使用 _.pick()/_.omit( )_.map(),并使用 _.uniqBy() 并将 rate 作为唯一标识符。

Typescript example (打开浏览器控制台)

const { flow, partialRight: pr, groupBy, map, head, pick, omit, uniqBy } = _

const EFF_DATA = 'effDate'
const baseProps = ['dc', EFF_DATA, 'expDate']

const fn = flow(
pr(groupBy, EFF_DATA),
pr(map, g => ({
...pick(head(g), baseProps),
rateCharge: uniqBy(map(g, pr(omit, baseProps)), 'rate')
}))
)

const data = [{"dc":1,"effDate":"1/2/2019","expDate":"1/2/2019","rate":1,"minCharge":2},{"dc":1,"effDate":"1/2/2019","expDate":"1/2/2019","rate":2,"minCharge":6},{"dc":1,"effDate":"1/2/2019","expDate":"1/2/2019","rate":4,"minCharge":7}]

const result = fn(data)

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

如果您已经导入了整个 lodash 包,您可以使用链接:

const { flow, partialRight: pr, groupBy, map, head, pick, omit, uniqBy } = _

const EFF_DATA = 'effDate'
const baseProps = ['dc', EFF_DATA, 'expDate']

const data = [{"dc":1,"effDate":"1/2/2019","expDate":"1/2/2019","rate":1,"minCharge":2},{"dc":1,"effDate":"1/2/2019","expDate":"1/2/2019","rate":2,"minCharge":6},{"dc":1,"effDate":"1/2/2019","expDate":"1/2/2019","rate":4,"minCharge":7}]

const result = _(data)
.groupBy(EFF_DATA)
.map(g => ({
...pick(head(g), baseProps),
rateCharge: uniqBy(map(g, pr(omit, baseProps)), 'rate')
}));

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

和 lodash/fp 版本:

const { flow, groupBy, map, head, pick, omit, uniqBy, assoc } = _;

const EFF_DATA = 'effDate'
const baseProps = ['dc', EFF_DATA, 'expDate'];

const fn = flow(
groupBy(EFF_DATA),
map(g => assoc(
'rateCharge',
flow(map(omit(baseProps)), uniqBy('rate'))(g),
pick(baseProps, head(g))
))
)

const data = [{"dc":1,"effDate":"1/2/2019","expDate":"1/2/2019","rate":1,"minCharge":2},{"dc":1,"effDate":"1/2/2019","expDate":"1/2/2019","rate":2,"minCharge":6},{"dc":1,"effDate":"1/2/2019","expDate":"1/2/2019","rate":4,"minCharge":7}]

const result = fn(data)

console.log(result)
<script src='https://cdn.jsdelivr.net/g/lodash@4(lodash.min.js+lodash.fp.min.js)'></script>

关于javascript - lodash 从对象数组中查找公共(public)值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54542902/

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