当前位置:网站首页>JS implements JSON array merging and de duplication
JS implements JSON array merging and de duplication
2022-06-09 15:09:00 【Don't stare at my name】
There are two json Array demo1 and demo2
var demo1 = [{"id": 0, "name": " beef "},{"id": 1,"name": " mutton "}];
var demo2 = [{"id": 2, "name": " beef "},{"id": 3,"name": " Fish "},{ "id": 4,"name":" chicken "}];Array merge
var totalDemo = demo1.concat(demo2);
console.log(totalDemo); //[{"id": 0, "name": " beef "},{"id": 1,"name": " mutton "},{"id": 2, "name": " beef "},{"id": 3,"name": " Fish "},{ "id": 4,"name":" chicken "}]Array merging uses concat Method , It can be used to connect strings and arrays .
Array weight removal
The merged array has been obtained above totalDemo , Get rid of name The attributes are the same json object
var temp = {}; // be used for name Judgment repeats
var result = []; // Last new array
totalDemo.map(function (item, index) {
if(!temp[item.name]){
result.push(item);
temp[item.name] = true;
}
});
console.log(result);//[{"id": 0, "name": " beef "},{"id": 1,"name": " mutton "},{"id": 3,"name": " Fish "},{ "id": 4,"name":" chicken "}];JSON Array de duplication uses the attribute name of the object to make judgments , Then get the new array , Is the array after de duplication .
JSON Array de duplication method encapsulation
const arr = [{"id": 0, "name": " beef "},{"id": 1,"name": " mutton "},{"id": 2, "name": " beef "},{"id": 3,"name": " Fish "},{ "id": 4,"name":" chicken "}]
/**
* JSON Array weight removal
* @params {Array} arr Incoming JSON Array
* @param {String} attrName According to which attribute name to duplicate
* @return {Array} Returns a new array after de duplication
* */
function delRepeatJson(arr = [], attrName = '') {
var temp = {}; // be used for name Judgment repeats
var result = []; // Last new array
arr.forEach(function (item, index) {
if (!temp[item[attrName]]) {
result.push(item);
temp[item[attrName]] = true;
}
});
return result;
}
console.log(delRepeatJson(arr, 'name')); // [{"id":0,"name":" beef "},{"id":1,"name":" mutton "},{"id":3,"name":" Fish "},{"id":4,"name":" chicken "}]边栏推荐
- 鸿蒙移植i.mx6ull(八) 内存映射(基于IMX6ULL)
- 文档编辑器
- tmux(Terminal MultipleXer)命令使用
- 金山云 Q1 营收 21.7 亿:其中公有云 13.8 亿、行业云 7.9 亿
- 酒店长租是一门好生意吗?
- ECCV 2020 | star: pedestrian trajectory prediction model based on transformer (II)
- 860. 柠檬水找零
- BAT和FAANG的时代翻篇儿了,谁来接棒?
- 微信小程序搜索框的代码写法
- China UnionPay (Cloud Computing) fixed-point services: lingqueyun, daocloud, Youyun and Boyun won the bid (Development); Shenzhou information, Tencent cloud and Xinhua three standards (transportation
猜你喜欢

【论文】Cascade R-CNN: Delving into High Quality Object Detection

為什麼 SQL 語句使用了索引,但卻還是慢查詢?

有奖调研

unity Dots的IComponent使用的列表踩的坑

@Enablefeignclients annotation source code analysis

The white paper on the panorama of the digital economy, bank wealth management, was released

Validate palindrome string

单调队列优化Dp例题

最成功也最差劲的CEO去世,索尼还是走在他的老路上

中金 | 数智中国之二:数据库商业市场五问五答
随机推荐
FCPX插件:动态物体运动模糊视觉特效Motion Blur FX by MA
[云原生]Kubernetes可视化界面WEBUI Kubernetes Dashboard
The panorama of yuancosmos industrial investment, fast step into the new era of yuancosmos!
华为哈勃将再添IPO,美芯晟蛰伏十余年后冲刺科创板
Error 1062 in database, error reporting
AE script - grid reference line golden section line generation tool guidesup! 2.2 active version
Critical area, event, mutex, semaphore -- four methods to control multithread synchronization and mutex
Geoffrey Hinton's latest interview: within five years, we will crack the brain's operating mechanism, but not through back propagation
ECCV 2020 | star: pedestrian trajectory prediction model based on transformer (II)
Hongmeng porting i.mx6ull (VIII) adding a board
混动大年,比亚迪的风评真要反转?
@EnableFeignClients注解源码解析
JS事件流、事件冒泡、阻止冒泡、事件捕获(一看就懂)
3 个技巧来破解你可以立即使用的 Flutter 生产力!
[cloud lesson] application platform lesson 43 cloud lesson gives you a quick introduction to multi cloud high availability services
mongoose连接多个数据库简单例子
Pourquoi l'instruction SQL utilise - t - elle un index, mais la requête est - elle lente?
AE脚本-网格参考线黄金分割线生成工具 GuidesUp! 2.2激活版
Halodoc使用 Apache Hudi 构建 Lakehouse的关键经验
微信小程序搜索框的代码写法