当前位置:网站首页>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
边栏推荐
- 1. Finishing huazi Mianjing -- 1
- [※ leetcode refers to offer 46. Translate numbers into strings (medium)]
- 电脑越用越慢怎么办?文件误删除恢复方法
- Tensorflow1. X and 2 How does x read those parameters saved in CKPT
- Chrome 94 引入具有争议的 Idle Detection API,苹果和Mozilla反对
- South Korea may ban apple and Google from offering commission to developers, the first in the world
- Detectron2 usage model
- go defer
- [leetcode refers to the maximum profit of offer 63. Stock (medium)]
- Keras. Layers introduction to various layers
猜你喜欢
管道和xargs
DeNO 1.13.2 release
亚马逊和Epic将入驻,微软应用商城向第三方开放
wait、waitpid
Express③(使用Express编写接口、跨域有关问题)
1. Finishing huazi Mianjing -- 1
Express ③ (use express to write interface and cross domain related issues)
Chrome 94 introduces the controversial idle detection API, which apple and Mozilla oppose
Another data analysis artifact: Polaris is really powerful
2.整理华子面经--2
随机推荐
matplotlib. Pyplot partition drawing
Automatic heap dump using MBean
airbase 初步分析
C knowledge
Detectron2 using custom datasets
亚马逊和Epic将入驻,微软应用商城向第三方开放
【SDU Chart Team - Core】SVG属性类设计之枚举
Deno 1.13.2 发布
Chrome 94 引入具有争议的 Idle Detection API,苹果和Mozilla反对
DeNO 1.13.2 release
[leetcode sword finger offer 10 - II. Frog jumping steps (simple)]
How to make Jenkins job run automatically after startup
Graph traversal - BFS, DFS
Arm architecture assembly instructions, registers and some problems
Centralized record of experimental problems
Getting started with detectron2
41. The first missing positive number
Selenium 显示等待WebDriverWait
setInterval、setTimeout、requestAnimationFrame