如何在javascript中检索数组中相同值的数量?
如何在javascript中检索数组中相同值的数量?,javascript,arrays,Javascript,Arrays,var数组=['box','box','line','time','box'] 例如:框重复了几次在这里: var array = ['box','box','line','time','box'], map = {}, key; array.forEach(function(item) { if (map[item] === undefined) { map[item] = 1; } else { map[item]++
var数组=['box','box','line','time','box']
例如:框重复了几次在这里:
var array = ['box','box','line','time','box'],
map = {},
key;
array.forEach(function(item) {
if (map[item] === undefined) {
map[item] = 1;
} else {
map[item]++;
}
});
for (key in map) {
console.log(" key:", key, " repeated:", map[key]);
}
循环、比较、计数?具有重复项的唯一值的数量?某个值存在的次数?或者重复值的总数?映射中的
项将优于==未定义的。