gpt4 book ai didi

javascript - UndescoreJS - 将对象属性转换为数组

转载 作者:行者123 更新时间:2023-11-28 18:37:22 25 4
gpt4 key购买 nike

情况很简单 - 我有一个对象列表:

[
{"id":"0001","status":"prod"},
{"id":"0002","status":"prod"},
{"id":"0003","status":"prod"},
{"id":"0004","status":"prod"},
{"id":"0005","status":"dev"},
{"id":"0006","status":"dev"}
]

当我使用_.groupBy('status')时,结果是:

{
prod: [{"id":"0001","status":"prod"},
{"id":"0002","status":"prod"},
{"id":"0003","status":"prod"},
{"id":"0004","status":"prod"}],
dev: [{"id":"0005","status":"dev"},
{"id":"0006","status":"dev"}]
}

但我需要像下面的代码那样的结果发送到 Chart.js(使用 Underscorejs):

{
legend: ['prod','dev'],
data: [4,2] // Array Length
}

如何使用 Underscore.js 解决我的问题?

最佳答案

在一条链中,只是为了好玩。

_.chain(data)
.groupBy('status')
.mapObject(x => x.length)
.pairs()
.unzip()
.zip(['legend', 'data'])
.map(x => x.reverse())
.object()
.value();

关于javascript - UndescoreJS - 将对象属性转换为数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36849992/

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