gpt4 book ai didi

javascript - 嵌套数组中的 Lodash 映射值

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

我有这种数据,我们通常从api获取,这是我第一次使用lodash。

{
"_id": "93866",
"stats": [
{
"points": 86,
"pos": 11,
"createdAt": "2019-01-16T10:13:40.650Z",
"updatedAt": "2019-01-16T10:13:40.825Z"
},
{
"points": 79,
"pos": 26,
"createdAt": "2019-01-16T10:13:40.650Z",
"updatedAt": "2019-01-16T10:13:40.825Z"
},
{
"points": 64,
"pos": 39,
"createdAt": "2019-01-16T10:13:40.650Z",
"updatedAt": "2019-01-16T10:13:40.825Z"
}
]
}

我想要的就是实现这样的目标

[ [ 86, 11 ], [ 79, 26 ], [ 64, 39 ] ]

我可以简单地以 native 方式执行此操作,

var result = data.stats.map(o => [o.points, o.pos])
// => [ [ 86, 11 ], [ 79, 26 ], [ 64, 39 ] ]

这会产生预期的结果,但我正在工作的环境取决于 lodash 的使用。

到目前为止我已经尝试过 _.map(data.stats, 'points') 但我不确定如何实现这样的 [ [ 86, 11 ], [ 79, 26], [ 64, 39]]

那么该怎么做呢?

最佳答案

您可以在函数回调中使用object#destructring

let data = {"_id":"93866","stats":[{"points":86,"pos":11,"createdAt":"2019-01-16T10:13:40.650Z","updatedAt":"2019-01-16T10:13:40.825Z"},{"points":79,"pos":26,"createdAt":"2019-01-16T10:13:40.650Z","updatedAt":"2019-01-16T10:13:40.825Z"},{"points":64,"pos":39,"createdAt":"2019-01-16T10:13:40.650Z","updatedAt":"2019-01-16T10:13:40.825Z"}]},
result = _.map(data.stats, ({points, pos}) => [points,pos]);
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>

关于javascript - 嵌套数组中的 Lodash 映射值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54217324/

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