JavaScript中统计数组中相同元素的个数-案例
图示说明
代码
//部分数据
[
"220",
"220",
"220",
"220",
"220",
"220",
"220",
"220",
"220",
"220",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
......
]
// 统计数组中相同元素的个数
let obj = {};
for (let i = 0; i < NFTIds.length; i++) {
if (obj[NFTIds[i]]) {
obj[NFTIds[i]]++;
} else {
obj[NFTIds[i]] = 1;
}
}
console.log(obj);
// 将相同元素的个数转换为数组
let arr = [];
for (let key in obj) {
arr.push({
id: key,
name: `${key}(${obj[key]})`,
});
}
console.log(arr);
本文来自博客园,作者:JackieDYH,转载请注明原文链接:https://www.cnblogs.com/JackieDYH/p/17634107.html