目前,我有一个数组如下:
var uniqueCount = Array();
几步之后,我的数组看起来像这样:
uniqueCount = [a,b,c,d,d,e,a,b,c,f,g,h,h,h,e,a];
我该如何计算数组中有多少个a、b、c?我希望得到这样的结果:a = 3
b = 1
c = 2
d = 2
目前,我有一个数组如下:
var uniqueCount = Array();
几步之后,我的数组看起来像这样:
uniqueCount = [a,b,c,d,d,e,a,b,c,f,g,h,h,h,e,a];
我该如何计算数组中有多少个a、b、c?我希望得到这样的结果:a = 3
b = 1
c = 2
d = 2
您可以不使用任何for/while循环或forEach解决它。
function myCounter(inputWords) {
return inputWords.reduce( (countWords, word) => {
countWords[word] = ++countWords[word] || 1;
return countWords;
}, {});
}
const obj = {};
const uniqueCount = [ 'a', 'b', 'c', 'd', 'e', 'a', 'b', 'c', 'f', 'g', 'h', 'h', 'h', 'e', 'a' ];
for (let i of uniqueCount) obj[i] ? obj[i]++ : (obj[i] = 1);
console.log(obj);
var counts = {};
uniqueCount.forEach(function(element) {
counts[element] = (counts[element] || 0) + 1;
});
for (var element in counts) {
console.log(element + ' = ' + counts[element]);
}
counts[element] || 0
? - Askingcounts[element]
会返回undefined
,因为该属性尚未具有值。如果您尝试添加undefined + 1
,则最终会得到NaN。(count[element] || 0)
将用0
替换undefined
,因此添加1
会产生1
而不是NaN
。ECMAScript 2020添加了nullish coalescing运算符??
,它执行类似的操作,但更明确地表示在第一个值为undefined
(或null
)时使用第二个值。该版本将是(counts[element] ?? 0) + 1
。 - nkron// new example.
var str= [20,1,-1,2,-2,3,3,5,5,1,2,4,20,4,-1,-2,5];
function findOdd(para) {
var count = {};
para.forEach(function(para) {
count[para] = (count[para] || 0) + 1;
});
return count;
}
console.log(findOdd(str));
uniqueCount = ['a','b','c','d','d','e','a','b','c','f','g','h','h','h','e','a'];
var map = new Object();
for(var i = 0; i < uniqueCount.length; i++) {
if(map[uniqueCount[i]] != null) {
map[uniqueCount[i]] += 1;
} else {
map[uniqueCount[i]] = 1;
}
}
uniqueCount = ["a","b","a","c","b","a","d","b","c","f","g","h","h","h","e","a"];
var count = {};
uniqueCount.forEach((i) => { count[i] = ++count[i]|| 1});
console.log(count);
步骤:首先检查累加器中是否有当前值,如果没有,则为该特定值设置计数为1;否则,如果累加器中已经存在该值,则简单地增加计数。
const testarr = [1,2,1,3,1,2,4];
var count = testarr.reduce((acc,currentval)=>{
if(acc[currentval]){ acc[currentval] = ++acc[currentval]; }else{ acc[currentval] = 1; } return acc; },{})
console.log(count);
包含字母的数组中的重复项:
var arr = ["a", "b", "a", "z", "e", "a", "b", "f", "d", "f"],
sortedArr = [],
count = 1;
sortedArr = arr.sort();
for (var i = 0; i < sortedArr.length; i = i + count) {
count = 1;
for (var j = i + 1; j < sortedArr.length; j++) {
if (sortedArr[i] === sortedArr[j])
count++;
}
document.write(sortedArr[i] + " = " + count + "<br>");
}
包含数字的数组中的重复元素:
var arr = [2, 1, 3, 2, 8, 9, 1, 3, 1, 1, 1, 2, 24, 25, 67, 10, 54, 2, 1, 9, 8, 1],
sortedArr = [],
count = 1;
sortedArr = arr.sort(function(a, b) {
return a - b
});
for (var i = 0; i < sortedArr.length; i = i + count) {
count = 1;
for (var j = i + 1; j < sortedArr.length; j++) {
if (sortedArr[i] === sortedArr[j])
count++;
}
document.write(sortedArr[i] + " = " + count + "<br>");
}
Str= ['a','b','c','d','d','e','a','h','e','a'];
var obj= new Object();
for(var i = 0; i < Str.length; i++) {
if(obj[Str[i]] != null) {
obj[Str[i]] += 1;
} else {
obj[Str[i]] = 1;
}
}
console.log(obj);
var counts = {};
var aarr=['a','b','a'];
aarr.forEach(x=>counts[x]=(counts[x] || 0)+1 );
console.log(counts)
{}
这种类型的map,而不是函数式编程中的map
。 - Matt Ball