gpt4 book ai didi

javascript - 下划线 : Find the most frequently occurring object in an array?

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:32:05 27 4
gpt4 key购买 nike

我这里有一个对象数组。

var list = [
{"id": 439, "name": "John"},
{"id": 439, "name": "John"},
{"id": 100, "name": "Kevin"},
{"id": 120, "name": "Max"},
{"id": 439, "name": "John"}
];

我需要从这个数组中提取最常出现的对象,并构建一个按最流行名称排序的新数组。

到目前为止,我已经尝试按照本主题的完成方式进行操作:Underscore.js: Find the most frequently occurring value in an array?

// Build temp list
temp_list = _(
_.chain(
_(list).pluck('id')
)
.countBy()
.pairs()
.value()
)
.sortBy(1)
.reverse();

// Build final list with most frequently occurring first
_.map(temp_list, function(current) {
return _.findWhere(list, {
'id': parseInt(current[0])
});
});

是否可以通过直接排序初始列表而不需要创建临时列表来改进此代码?

最佳答案

您差不多明白了:您可以在 reverse 调用后立即调用 map。这是我的做法:

var newlist = _.chain(list)
.countBy(function (item) { return item.id; })
.pairs()
.sortBy(function (item) { return item[1]; })
.reverse()
.map(function (item) { return _.findWhere(list, { id: parseInt(item[0]) }); })
.value();

分割:

chain: returns a wrapped version of the array, that let's you chain underscore functions.

countBy: returns an object where the keys are whatever value is returned from the callback, and the values are the number of times those keys occured.

pairs: converts { key: 'value' } to ['key', 'value']

sortBy: returns an array sorted by the value returned from the callback

reverse: reverses the array

map: returns a new array where each item is based on the item in the original array at that index and whatever is done to that value in the callback. In this case, we're using the id (item[0]) to get the object from the original list.

value: unwraps the chain-able object and returns the "raw" value.

关于javascript - 下划线 : Find the most frequently occurring object in an array?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37186622/

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