当前位置:网站首页>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
边栏推荐
- Resolve the "chromedriver executable needs to be in path" error
- 使用mbean 自动执行heap dump
- Question brushing plan - depth first search DFS (I)
- Tensorflow realizes gradient accumulation, and then returns
- Tencent cloud has two sides in an hour, which is almost as terrible as one side..
- Pikachuxss how to get cookie shooting range, always fail to return to the home page
- mmap、munmap
- Centralized record of experimental problems
- FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ‘:app:stripDe
- 1.整理华子面经--1
猜你喜欢
![[leetcode refers to offer 47. Maximum value of gift (medium)]](/img/b4/34b3c74516e3b1ba93b7d84916dadc.png)
[leetcode refers to offer 47. Maximum value of gift (medium)]

1.整理华子面经--1

Introduction to tensorrt


电脑越用越慢怎么办?文件误删除恢复方法
![[leetcode refers to the two numbers of offer 57. And S (simple)]](/img/c2/7c1434e3c0bbcc6d6364361c3a3ab4.png)
[leetcode refers to the two numbers of offer 57. And S (simple)]

IIS cannot load * woff,*. woff2,*. Solution of SVG file

airbase 初步分析

Write table of MySQL Foundation (create table)

Google tries to use rust in Chrome
随机推荐
Deep analysis of C language function
Common commands of MySQL in Linux
Minecraft 1.12.2模组开发(四十三) 自定义盾牌(Shield)
Ubuntu 20 installing centernet
Rust更适合经验较少的程序员?
Presto on spark supports 3.1.3 records
Valueerror: invalid literal for int() with base 10 conversion error related to data type
Express ③ (use express to write interface and cross domain related issues)
Pycharm download and installation
C#,打印漂亮的贝尔三角形(Bell Triangle)的源程序
Minecraft 1.12.2 module development (43) custom shield
Normalized transforms in pytorch The real calculation process of normalize
airbase 初步分析
Reference of custom message in ROS function pack failed
Unit function expansion
Sequential state
Thread safe sigleton (singleton mode)
Two Stage Detection
【SDU Chart Team - Core】SVG属性类设计之枚举
Centos7 builds MySQL master-slave replication from scratch (avoid stepping on the pit)