当前位置:网站首页>JS merge duplicate data in array object
JS merge duplicate data in array object
2022-04-23 21:24:00 【Sister Chunfeng】
Array reorganization data
Source data :
Target data :
// Source data
var oldData = [
{
city_id: 1,
city_name: ' Beijing ',
city_img: "http://dfknbdjknvkjsfnvlkjdn.png",
city_country: " China "
},
{
city_id: 2,
city_name: ' Shanghai ',
city_img: "http://wergerbe.png",
city_country: " China "
},
{
city_id: 3,
city_name: ' Guangzhou ',
city_img: "http://hrthhr.png",
city_country: " China "
},
{
city_id: 4,
city_name: ' Seattle ',
city_img: "http://frevfd.png",
city_country: " The United States "
},
{
city_id: 5,
city_name: ' New York ',
city_img: "http:// Instead .png",
city_country: " The United States "
}
]
// The rule of turning source data into target data first
var oldDataRule = []
oldData.forEach(el => {
var oldObj = {
name: el.city_country,
citys:[]
}
var cityObj = {
city_name: el.city_name,
city_img: el.city_img,
city_id: el.city_id
}
oldObj.citys.push(cityObj)
oldDataRule.push(oldObj)
})
/** * Let's go first , After the merger * 1、 Source data De duplication * 2、 The de duplicated data is the same as the source data name Data consolidation of citys */
var newData = []
var newObj = {
}
oldDataRule.forEach((el, i) => {
if (!newObj[el.name]) {
newData.push(el);
newObj[el.name] = true;
} else {
newData.forEach(el => {
if (el.name === oldDataRule[i].name) {
el.citys = el.citys.concat(oldDataRule[i].citys);
// el.citys = [...el.citys, ...oldDataRule[i].citys]; // es6 grammar
}
})
}
})
console.log(newData); // Target data
result :
版权声明
本文为[Sister Chunfeng]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/110/202204200620163928.html
边栏推荐
- Reference of custom message in ROS function pack failed
- Express③(使用Express编写接口、跨域有关问题)
- Google tries to use rust in Chrome
- Chrome 94 引入具有争议的 Idle Detection API,苹果和Mozilla反对
- Leetcode-279-complete square number
- Common commands of MySQL in Linux
- [leetcode sword finger offer 10 - II. Frog jumping steps (simple)]
- Preliminary analysis of Airbase
- Pytorch preserves different forms of pre training models
- Realrange, reduce, repeat and einops in einops package layers. Rearrange and reduce in torch. Processing methods of high-dimensional data
猜你喜欢

亚马逊和Epic将入驻,微软应用商城向第三方开放

1. Finishing huazi Mianjing -- 1

C#,打印漂亮的贝尔三角形(Bell Triangle)的源程序

What about laptop Caton? Teach you to reinstall the system with one click to "revive" the computer

Question brushing plan - depth first search (II)

Google 尝试在 Chrome 中使用 Rust

Deep analysis of C language function

又一款数据分析神器:Polars 真的很强大

Use 3080ti to run tensorflow GPU = 1 X version of the source code

wait、waitpid
随机推荐
airbase 初步分析
ubutnu20安裝CenterNet
Google 尝试在 Chrome 中使用 Rust
Keywords static, extern + global and local variables
Leetcode-279-complete square number
阿里又一个“逆天”容器框架!这本Kubernetes进阶手册简直太全了
thinkphp5+数据大屏展示效果
YOLOv5 Unable to find a valid cuDNN algorithm to run convolution
2.整理华子面经--2
C, print the source program of beautiful bell triangle
Yolov5 NMS source code understanding
Some grounded words
mmap、munmap
Prim、Kruskal
Xiaomi mobile phone has abandoned the "Mi" brand all over the world and switched to the full name brand of "Xiaomi"
Selenium 显示等待WebDriverWait
Send email to laravel
go interface
The more you use the computer, the slower it will be? Recovery method of file accidental deletion