在Javascript中,我试图采取数字值的初始数组,并计算其中的元素。理想情况下,结果将是两个新数组,第一个数组指定每个唯一元素,第二个数组包含每个元素出现的次数。但是,我愿意听取关于输出格式的建议。
例如,如果初始数组是:
5, 5, 5, 2, 2, 2, 2, 2, 9, 4
然后将创建两个新数组。第一个将包含每个唯一元素的名称:
5, 2, 9, 4
第二个将包含该元素在初始数组中出现的次数:
3, 5, 1, 1
因为数字5在初始数组中出现了三次,数字2出现了五次,9和4都出现了一次。
我一直在寻找解决方案,但似乎没有一个可行,而且我自己尝试过的每件事最后都出奇地复杂。任何帮助都将不胜感激!
谢谢:)
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
}
这个问题已经有8年的历史了,很多很多的答案都没有考虑到ES6及其众多的优势。
也许更重要的是,当我们创建额外的数组,创建数组的两倍或三倍副本,甚至将数组转换为对象时,考虑我们的代码对垃圾收集/内存管理的后果。对于小型应用来说,这些都是微不足道的观察结果,但如果规模化是一个长期目标,那么就要彻底考虑这些问题。
如果您只是需要一个特定数据类型的“计数器”,并且起点是一个数组(我假设您因此想要一个有序列表,并利用数组提供的许多属性和方法),那么您只需简单地遍历array1,并用array1中找到的这些值的值和出现次数填充array2。
就这么简单。
面向对象编程和面向对象设计的简单类SimpleCounter (ES6)的示例
class SimpleCounter {
constructor(rawList){ // input array type
this.rawList = rawList;
this.finalList = [];
}
mapValues(){ // returns a new array
this.rawList.forEach(value => {
this.finalList[value] ? this.finalList[value]++ : this.finalList[value] = 1;
});
this.rawList = null; // remove array1 for garbage collection
return this.finalList;
}
}
module.exports = SimpleCounter;
Const arr = [2,2,5,2,2,2,4,5,5,9];
函数foo(数组){
Let a = [],
B = [],
Arr =…, //克隆数组,这样我们在使用.sort()时不会改变原始数组
prev;
arr.sort ();
For (let元素的arr) {
If (element !== prev) {
a.push(元素);
b.push (1);
}
+ + b (b。长度- 1];
Prev =元素;
}
返回[a, b];
}
Const result = foo(arr);
console.log('(' +结果[0 ] + ']','[' + 结果[1]+ ')')
console.log (arr)
给定下面提供的数组:
const array = [ 'a', 'b', 'b', 'c', 'c', 'c' ];
你可以使用这个简单的一行代码来生成一个哈希映射,将一个键链接到它在数组中出现的次数:
const hash = Object.fromEntries([ ...array.reduce((map, key) => map.set(key, (map.get(key) || 0) + 1), new Map()) ]);
// { a: 1, b: 2, c: 3 }
扩展和解释:
// first, we use reduce to generate a map with values and the amount of times they appear
const map = array.reduce((map, key) => map.set(key, (map.get(key) || 0) + 1), new Map())
// next, we spread this map into an array
const table = [ ...map ];
// finally, we use Object.fromEntries to generate an object based on this entry table
const result = Object.fromEntries(table);
这个数组归功于@corashina。减少代码
你可以使用一个对象来保存结果:
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"]