在Javascript中,我试图采取数字值的初始数组,并计算其中的元素。理想情况下,结果将是两个新数组,第一个数组指定每个唯一元素,第二个数组包含每个元素出现的次数。但是,我愿意听取关于输出格式的建议。

例如,如果初始数组是:

5, 5, 5, 2, 2, 2, 2, 2, 9, 4

然后将创建两个新数组。第一个将包含每个唯一元素的名称:

5, 2, 9, 4

第二个将包含该元素在初始数组中出现的次数:

3, 5, 1, 1

因为数字5在初始数组中出现了三次,数字2出现了五次,9和4都出现了一次。

我一直在寻找解决方案,但似乎没有一个可行,而且我自己尝试过的每件事最后都出奇地复杂。任何帮助都将不胜感激!

谢谢:)


当前回答

Const data = [5,5,5,2,2,2,2,2,2,2,2,9,4] 函数countAndSort(arr) { 返回Object.entries(加勒比海盗。减少((上一页,咕咕叫)= >(上一页[咕咕叫]= + +上一页[咕咕叫]| | 1,上一页),{})).sort (b (a, b) = >[1]——[1]) } console.log (countAndSort(数据)

其他回答

Const data = [5,5,5,2,2,2,2,2,2,2,2,9,4] 函数计数(arr) { 加勒比海盗。减少((上一页,咕咕叫)= >(上一页[咕咕叫]= + +上一页[咕咕叫]| | 1,上一页),{}) } console.log (count(数据)

返回一个可排序的数组:

let array = [5, 5, 5, 2, 2, 2, 2, 2, 9, 4] let reducedArray = array.reduce( (acc, curr, _, arr) => { if (acc.length == 0) acc.push({item: curr, count: 1}) else if (acc.findIndex(f => f.item === curr ) === -1) acc.push({item: curr, count: 1}) else ++acc[acc.findIndex(f => f.item === curr)].count return acc }, []); console.log(reducedArray.sort((a,b) => b.count - a.count )) /* Output: [ { "item": 2, "count": 5 }, { "item": 5, "count": 3 }, { "item": 9, "count": 1 }, { "item": 4, "count": 1 } ] */

如果你喜欢单线。

加勒比海盗。reduce(function(countMap, word) {countMap[word] = ++countMap[word] || 1;return countMap}, {});

编辑(6/12/2015): 由内而外的解释。 countMap是一个映射一个单词的频率的映射,我们可以看到匿名函数。reduce所做的是应用带有参数的函数作为所有数组元素,并将countMap作为最后一次函数调用的返回值传递。最后一个参数({})是第一次函数调用时countMap的默认值。

2021年的版本

更优雅的方法是使用逻辑空赋值(x ??= y)结合数组#reduce()与O(n)时间复杂度。

主要思想仍然是使用array# reduce()将输出作为对象进行聚合,以获得最高的性能(时间和空间复杂度),就像其他答案一样,搜索和构造中间数组。

Const arr = [2,2,2,2,2,2,4,5,5,5,9]; Const result = arr。Reduce ((acc, curr) => { acc(咕咕叫)? ?= {[curr]: 0}; acc[咕咕叫][咕咕叫]+ +; 返回acc; }, {}); console.log (Object.values(结果));

清理和重构代码

使用逗号操作符(,)语法。

逗号操作符(,)计算它的每个操作数(从左到右) 右)并返回最后一个操作数的值。

Const arr = [2,2,2,2,2,2,4,5,5,5,9]; Const result = arr。减少((acc,咕咕叫)= > (acc[咕咕叫]= (acc(咕咕叫)| | 0)+ 1,acc), {}); console.log(结果);

输出

{
  "2": 5,
  "4": 1,
  "5": 3,
  "9": 1
}

你可以使用一个对象来保存结果:

Const arr = [5,5,5,2,2,2,2,2,2,2,2,9,4]; Const counts = {}; (const num of arr) { Counts [num] = Counts [num] ?计数[num] + 1: 1; } console.log(重要); Console.log(计数[5],计数[2],计数[9],计数[4]);

所以,现在你的counts对象可以告诉你一个特定数字的计数是多少:

console.log(counts[5]); // logs '3'

如果您想获取成员数组,只需使用keys()函数即可

keys(counts); // returns ["5", "2", "9", "4"]