当前位置:网站首页>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
边栏推荐
- Xiaohei leetcode's refreshing rainy day trip, just finished eating Yufei Beef Noodles, Mala Tang and Beer: 112. Path Sum
- Tensorflow模型整体构建流程
- APP automation test framework - UiAutomator2 introductory
- mysql multi-table left link query
- Synchronization lock synchronized traces the source
- Multiple reasons for MySQL slow query
- [Implementation of the interface for adding, deleting, checking, and modifying a double-linked list]
- json事例
- 聊聊SQL语句中 DDL 、DML 、DQL 、DCL 分别是什么
- PHP 二维数组根据某个字段排序
猜你喜欢
随机推荐
Metasploit常用命令、技术功能模块
random.normal() and random.truncated_normal() in TF
TF中random.normal()与random.truncated_normal()
Pagoda measurement - building LightPicture open source map bed system
The round functions in the np, ceil function and floor function
阿里云架构师金云龙:基于云XR平台的视觉计算应用部署
abstract class or interface
js十五道面试题(含答案)
深度剖析 Apache EventMesh 云原生分布式事件驱动架构
JS–比想象中简单
TF生成均匀分布的tensor
AI识万物:从0搭建和部署手语识别系统
nvm下node安装;node环境变量配置
任务流执行器是如何工作的?
【微服务~Nacos】Nacos之配置中心
SecureCRT background color
TRUNCATE表之后空间未释放
CVPR22 Oral | shunt through multi-scale token polymerization from attention, code is open source
This article lets you quickly understand implicit type conversion [integral promotion]!
laravel table migration error [easy to understand]