在Javascript中,我试图采取数字值的初始数组,并计算其中的元素。理想情况下,结果将是两个新数组,第一个数组指定每个唯一元素,第二个数组包含每个元素出现的次数。但是,我愿意听取关于输出格式的建议。
例如,如果初始数组是:
5, 5, 5, 2, 2, 2, 2, 2, 9, 4
然后将创建两个新数组。第一个将包含每个唯一元素的名称:
5, 2, 9, 4
第二个将包含该元素在初始数组中出现的次数:
3, 5, 1, 1
因为数字5在初始数组中出现了三次,数字2出现了五次,9和4都出现了一次。
我一直在寻找解决方案,但似乎没有一个可行,而且我自己尝试过的每件事最后都出奇地复杂。任何帮助都将不胜感激!
谢谢:)
所以这里是我如何做的一些最新的javascript功能:
首先,将数组缩减为一个计数的Map:
let countMap = array.reduce(
(map, value) => {map.set(value, (map.get(value) || 0) + 1); return map},
new Map()
)
通过使用Map,您的起始数组可以包含任何类型的对象,并且计数将是正确的。如果没有Map,某些类型的对象会给你奇怪的计数。
关于区别的更多信息,请参阅Map文档。
如果你所有的值都是符号、数字或字符串,这也可以用在对象上:
let countObject = array.reduce(
(map, value) => { map[value] = (map[value] || 0) + 1; return map },
{}
)
或者使用解构和对象扩展语法,以一种没有突变的函数式方式略显花哨:
let countObject = array.reduce(
(value, {[value]: count = 0, ...rest}) => ({ [value]: count + 1, ...rest }),
{}
)
此时,您可以使用Map或对象进行计数(与对象不同,Map是直接可迭代的),或者将其转换为两个数组。
有关地图:
countMap.forEach((count, value) => console.log(`value: ${value}, count: ${count}`)
let values = countMap.keys()
let counts = countMap.values()
或者对于对象:
Object
.entries(countObject) // convert to array of [key, valueAtKey] pairs
.forEach(([value, count]) => console.log(`value: ${value}, count: ${count}`)
let values = Object.keys(countObject)
let counts = Object.values(countObject)
查看下面的代码。
<html>
<head>
<script>
// array with values
var ar = [5, 5, 5, 2, 2, 2, 2, 2, 9, 4];
var Unique = []; // we'll store a list of unique values in here
var Counts = []; // we'll store the number of occurances in here
for(var i in ar)
{
var Index = ar[i];
Unique[Index] = ar[i];
if(typeof(Counts[Index])=='undefined')
Counts[Index]=1;
else
Counts[Index]++;
}
// remove empty items
Unique = Unique.filter(function(){ return true});
Counts = Counts.filter(function(){ return true});
alert(ar.join(','));
alert(Unique.join(','));
alert(Counts.join(','));
var a=[];
for(var i=0; i<Unique.length; i++)
{
a.push(Unique[i] + ':' + Counts[i] + 'x');
}
alert(a.join(', '));
</script>
</head>
<body>
</body>
</html>
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)
不要使用两个数组的结果,使用一个对象:
a = [5, 5, 5, 2, 2, 2, 2, 2, 9, 4];
result = { };
for(var i = 0; i < a.length; ++i) {
if(!result[a[i]])
result[a[i]] = 0;
++result[a[i]];
}
然后结果将看起来像:
{
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"]
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(数据)