当前位置:网站首页>Use js to count the number of occurrences of each string in the string array, and format it into an object array.
Use js to count the number of occurrences of each string in the string array, and format it into an object array.
2022-07-26 07:47:00 【Want to be a happy repair dog】
1. demand
- The original array :
[‘ Steamed stuffed bun ’, ‘ Steamed bread ’, ‘ Hanamaki ’, ‘ Steamed Bun Stuffed with Juicy Pork ’, ‘ Steamed bread ’]
- Format it as :
[
{ name: ‘ Steamed stuffed bun ’, value: 1 },
{ name: ‘ Steamed bread ’, value: 2 },
{ name: ‘ Hanamaki ’, value: 1 },
{ name: ‘ Steamed Bun Stuffed with Juicy Pork ’, value: 1 }
]
2. Realization
2.1 Statistics string array , The number of occurrences of each string , Form hash table
let arr = [" Steamed stuffed bun ", " Steamed bread ", " Hanamaki ", " Steamed Bun Stuffed with Juicy Pork ", " Steamed bread "];
let map = {
};
// Traverse list data
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);
result :
2.2 Traversal hash table , Format it
let resList = [];
// Traverse hash surface
for (let name in map) {
let item = {
};
item["name"] = name;
item["value"] = map[name];
resList.push(item);
}
console.log(resList);
result :
3. Complete code
let arr = [" Steamed stuffed bun ", " Steamed bread ", " Hanamaki ", " Steamed Bun Stuffed with Juicy Pork ", " Steamed bread "];
let map = {
};
// Traverse list data
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 = [];
// Traverse hash surface
for (let name in map) {
let item = {
};
item["name"] = name;
item["value"] = map[name];
resList.push(item);
}
console.log(resList);
边栏推荐
猜你喜欢

Unity Metaverse(二)、Mixamo & Animator 混合树与动画融合

2021全球机器学习大会演讲稿

Command line execution and test report generation of JMeter performance test

Idea settings set shortcut keys to convert English letters to case in strings

JWT快速入门

时间序列分析预测实战之ARIMA模型

20220209 create a basic Servlet

Parameterization of JMeter performance test using CSV file

MySQL之执行计划

元宇宙基础设施:WEB 3.0 chain33 优势分析
随机推荐
1. MySQL Architecture [MySQL advanced]
爬虫->TpImgspider
Next item recommendations in short sessions
Jmeter性能测试之命令行执行和生成测试报告
《门锁》引爆独居安全热议 全新海报画面令人窒息
Keras learning part: obtaining the output results of neural network middle layer
Parameterization of JMeter performance test using CSV file
现在开发人员都开始做测试了,是不是以后就没有软件测试人员了?
Basic knowledge of convolutional neural network
数据库基础
[classic thesis of recommendation system (10)] Alibaba SDM model
Learning Efficient Convolutional Networks Through Network Slimming
July training (day 18) - tree
元宇宙基础设施:WEB 3.0 chain33 优势分析
Now developers are beginning to do testing. Will there be no software testers in the future?
NLP natural language processing - Introduction to machine learning and natural language processing (3)
20220209 create a basic Servlet
dcn(deep cross network)三部曲
Database foundation
时间序列分析预测实战之ARIMA模型