当前位置:网站首页>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

The

set 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

The

reduce() 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

原网站

版权声明
本文为[m0_67393828]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/221/202208092000354407.html