当前位置:网站首页>利用js实现统计字符串数组中各字符串出现的次数,并将其格式化为对象数组。
利用js实现统计字符串数组中各字符串出现的次数,并将其格式化为对象数组。
2022-07-26 07:45:00 【想做一只快乐的修狗】
1. 需求
- 原始数组:
[‘包子’, ‘馒头’, ‘花卷’, ‘汤包’, ‘馒头’]
- 将其格式化为:
[
{ name: ‘包子’, value: 1 },
{ name: ‘馒头’, value: 2 },
{ name: ‘花卷’, value: 1 },
{ name: ‘汤包’, value: 1 }
]
2. 实现
2.1 统计字符串数组中,各字符串出现的次数,构成哈希表
let arr = ["包子", "馒头", "花卷", "汤包", "馒头"];
let map = {
};
// 遍历列表数据
for (let i = 0; i < arr.length; i++) {
var name = arr[i];
if (map[name]) {
map[name] += 1;
} else {
map[name] = 1;
}
}
console.log(map);
结果:
2.2 遍历哈希表,对其进行格式化
let resList = [];
// 遍历hash表
for (let name in map) {
let item = {
};
item["name"] = name;
item["value"] = map[name];
resList.push(item);
}
console.log(resList);
结果:
3. 完整代码
let arr = ["包子", "馒头", "花卷", "汤包", "馒头"];
let map = {
};
// 遍历列表数据
for (let i = 0; i < arr.length; i++) {
var name = arr[i];
if (map[name]) {
map[name] += 1;
} else {
map[name] = 1;
}
}
console.log(map);
let resList = [];
// 遍历hash表
for (let name in map) {
let item = {
};
item["name"] = name;
item["value"] = map[name];
resList.push(item);
}
console.log(resList);
边栏推荐
- OVS underlying implementation principle
- PHP environment deployment
- [200 opencv routines] 231. Gray level co-occurrence matrix (GLCM) for feature description
- The bigger the project is, the bigger it is. This is how I split it
- Como automatic test system: build process record
- Installation of Baidu flying paste deep learning framework tutorial in Anaconda
- C # use log4net to record logs (basic chapter)
- 《门锁》引爆独居安全热议 全新海报画面令人窒息
- 元宇宙基础设施:WEB 3.0 chain33 优势分析
- 从Boosting谈到LamdaMART
猜你喜欢

Installation of Baidu flying paste deep learning framework tutorial in Anaconda
![[uniapp] encapsulation of multiple payment methods](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[uniapp] encapsulation of multiple payment methods

WCF deployed on IIS

Hcip--- MPLS detailed explanation and BGP route filtering

Wrong Addition

Taishan office lecture: word error about inconsistent values of page margins

Dynamic performance view overview

PXE efficient batch network installation

Regular expression rules and common regular expressions

What is message subscription and publishing?
随机推荐
What is bloom filter in redis series?
在线问题反馈模块实战(十四):实现在线答疑功能
Comparison and difference between dependence and Association
What is message subscription and publishing?
Hcip--- BGP comprehensive experiment
NFS service and Samba service deployment
Logical volume management (LVM)
DADNN: Multi-Scene CTR Prediction via Domain-Aware Deep Neural Network
PHP environment deployment
Network trimming: a data driven neuron pruning approach towards efficient deep architectures paper translation / Notes
KDD2022 | 揭秘快手短视频推荐Re-ranking之谜,相关推荐新SOTA
“尝鲜”元宇宙,周杰伦最佳拍档方文山将于7月25日官宣《华流元宇宙》
Enterprise private network construction and operation and maintenance
[uniapp] encapsulation of multiple payment methods
记一次路由器频繁掉线问题的分析、解决与发展
Master slave database deployment
Lnmp+wordpress to quickly build a personal website
【Keras入门日志(3)】Keras中的序贯(Sequential)模型与函数式(Functional)模型
Basic knowledge of convolutional neural network
Devaxpress.xtraeditors.datanavigator usage