当前位置:网站首页>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
边栏推荐
- String hashing (2014 SERC J question)
- Evolution of MLOps
- SQLi-LABS Page-2 (Adv Injections)
- 聊天尬死名场面,你遇到过吗?教你一键获取斗图表情包,晋升聊天达人
- JS Deobfuscation - AST Restoration Case
- STC8H development (15): GPIO drive Ci24R1 wireless module
- SQLi-LABS Page-2 (Adv Injections)
- SecureCRT sets the timeout period for automatic disconnection
- 【双链表增删查改接口的实现】
- 发送激活邮件「建议收藏」
猜你喜欢
【微服务~Nacos】Nacos服务提供者和服务消费者
AI+医疗:使用神经网络进行医学影像识别分析
JS解混淆-AST还原案例
一文让你快速了解隐式类型转换【整型提升】!
从产品角度看 L2 应用:为什么说这是一个游乐场?
In-depth analysis of Apache EventMesh cloud-native distributed event-driven architecture
Five Star Holdings Wang Jianguo: Deepen the track with "plant spirit" and promote growth with "animal spirit"
MySQL慢查询的多个原因
金山云地震,震源在字节?
在VMware上安装win虚拟机
随机推荐
论文解读(DropEdge)《DropEdge: Towards Deep Graph Convolutional Networks on Node Classification》
从产品角度看 L2 应用:为什么说这是一个游乐场?
级联下拉菜单的实现「建议收藏」
Technology Sharing | How to Handle Header Cookies in Interface Automation Testing
POWER SOURCE ETA埃塔电源维修FHG24SX-U概述
Under the NVM node installation;The node environment variable configuration
js数组对象去重
JS解混淆-AST还原案例
【微服务~Nacos】Nacos服务提供者和服务消费者
js十五道面试题(含答案)
Leetcode 93 IP addresses
聊天尬死名场面,你遇到过吗?教你一键获取斗图表情包,晋升聊天达人
台风生成,广州公交站场积极开展台风防御安全隐患排查
Sudoku | Backtrack-7
Converting angles to radians
用户代码未处理MetadataException
4D Summary: 38 Knowledge Points of Distributed Systems
Evolution of MLOps
nvm下node安装;node环境变量配置
单元测试