当前位置:网站首页>js array object deduplication
js array object deduplication
2022-08-09 23:44:00 【m0_67393828】
Today, I will introduce three methods for deduplication of array objects, which are simple and convenient.
There is such a group of array objects:
let arr = [{id: '1',key: '1',value: 'Moon'}, {id: '3',key: '2',value: 'Kexin'}, {id: '2',key: '3',value: 'Little Red'}, {id: '1',key: '1',value: 'Xiaoxin'}, {id: '1',key: '2',value: 'Xiaojing'}]
1. Methods for accessing properties of objects
let newArr = [];let obj = {};for (var i = 0; i < arr.length; i++) {if (!obj[arr[i].key]) {newArr.push(arr[i])obj[arr[i].key] = true}}console.log(newArr);
2, Map() method
Theset method sets the key value corresponding to the key, and then returns the entire Map structure.If the key already has a value, the key value will be updated, otherwise the key will be newly generated.
The values method can return a traverser object of the Map object's values
let map = new Map();for (let item of this.arr) {map.set(item.id, item);}this.arr = [...map.values()];console.log(this.arr)
3, reduce() method
Thereduce() method receives a function as an accumulator, and each value in the array (from left to right) starts to reduce, and finally calculates to a value
array.reduce(function(total, currentValue, currentIndex, arr), initialValue)
Parameters
Description
total
Required.The initial value, or the return value after the calculation ends.
currentValue
Required.current element
currentIndex
Optional.the index of the current element
arr
Optional.The array object to which the current element belongs.
initialValue
Optional.The initial value passed to the function
const obj = {}arr = arr.reduce((total, next) => {obj[next.key] ? '' : obj[next.key] = true && total.push(next)return total}, [])console.log(arr)
There is another requirement here. If there are two or more judgment conditions, de-duplicate the array object and add a judgment condition.
const hasObj = {}arr = arr.reduce((total, next) => {const filterKey = next.key + next.id;hasObj[filterKey] ? "" : hasObj[filterKey] = true && total.push(next)return total}, [])console.log(arr)
Hope this helps you!
Let me introduce myself first. The editor graduated from Shanghai Jiaotong University in 2013. I worked in a small company and went to big factories such as Huawei and OPPO. I joined Ali in 2018, until now.I know that most junior and intermediate java engineers want to upgrade their skills, they often need to explore their own growth or sign up to study, but for training institutions, the tuition fee is nearly 10,000 yuan, which is really stressful.Self-learning that is not systematic is very inefficient and lengthy, and it is easy to hit the ceiling and the technology stops.Therefore, I collected a "full set of learning materials for java development" for everyone. The original intention is very simple. I hope to help friends who want to learn by themselves but don't know where to start, and at the same time reduce everyone's burden.Add the business card below to get a full set of learning materials
边栏推荐
- 基于ABP的AppUser对象扩展
- PHP 2D array sorted by a field
- Domestic mobile phone manufacturers once fought for it, but now it is the first to collapse...
- Metasploit常用命令、技术功能模块
- 聊天尬死名场面,你遇到过吗?教你一键获取斗图表情包,晋升聊天达人
- Interpretation of the paper (DropEdge) "DropEdge: Towards Deep Graph Convolutional Networks on Node Classification"
- 技术分享 | 接口自动化测试之JSON Schema模式该如何使用?
- MySQL慢查询的多个原因
- APP自动化测试框架-UiAutomator2基础入门
- [Cloud Native] 4.2 DevOps Lectures
猜你喜欢
小程序+自定义插件的关键性
JS Deobfuscation - AST Restoration Case
金山云地震,震源在字节?
腾讯继续挥舞降本增效“大刀”,外包员工免费餐饮福利被砍了
6个规则去净化你的代码
AI+Medical: Using Neural Networks for Medical Image Recognition and Analysis
【云原生】4.2 DevOps 精讲篇
Evolution of MLOps
Five Star Holdings Wang Jianguo: Deepen the track with "plant spirit" and promote growth with "animal spirit"
ACM MM 2022 | Cloud2Sketch: 长空云作画,AI笔生花
随机推荐
json事例
发送激活邮件「建议收藏」
AI+Medical: Using Neural Networks for Medical Image Recognition and Analysis
Flask入门学习教程
聊聊SQL语句中 DDL 、DML 、DQL 、DCL 分别是什么
MLOps的演进历程
APP automation test framework - UiAutomator2 introductory
TF生成均匀分布的tensor
laravel table migration error [easy to understand]
JS Deobfuscation - AST Restoration Case
Blender程序化建模简明教程【PCG】
从产品角度看 L2 应用:为什么说这是一个游乐场?
【EF】 更新条目时出错。有关详细信息,请参见内部异常。[通俗易懂]
AI Knows Everything: Building and Deploying a Sign Language Recognition System from Zero
Use zeros(), ones(), fill() methods to generate data in TF
国内手机厂商曾为它大打出手,如今它却最先垮台……
CVPR22 Oral|通过多尺度token聚合分流自注意力,代码已开源
【微服务~Nacos】Nacos服务提供者和服务消费者
“稚晖君”为2022昇腾AI创新大赛打call&nbsp;期待广大开发者加入
编译原理之文法