gpt4 book ai didi

javascript - js lodash 按更深的字段排序

转载 作者:可可西里 更新时间:2023-11-01 02:55:47 25 4
gpt4 key购买 nike

我有一个对象数组,我想按更深的字段进行排序。我该怎么做?

var array =    
{
"id": 27,
"name": "La Primita Product",
"lottery": {
"id": 1,
"name": "La Primitiva",
"jackpotAmount": 10000,
}
},
{
"id": 28,
"name": "La Primita Product",
"lottery": {
"id": 1,
"name": "La Primitiva",
"jackpotAmount": 10000,
}

如何按 lottery.jackpotAmount 排序?

最佳答案

收件人_.sortBy你可以将回调作为第二个参数传递,就像这样

var array = [{
"id": 27,
"name": "La Primita Product",
"lottery": {
"id": 1,
"name": "La Primitiva",
"jackpotAmount": 10000,
}
}, {
"id": 28,
"name": "La Primita Product",
"lottery": {
"id": 1,
"name": "La Primitiva",
"jackpotAmount": 10,
}
}];

array = _.sortBy(array, function (el) {
return el.lottery.jackpotAmount;
});

console.log(array);
<script src="//cdnjs.cloudflare.com/ajax/libs/lodash.js/2.4.1/lodash.min.js"></script>

关于javascript - js lodash 按更深的字段排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27526612/

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