Home > Backend Development > PHP Tutorial > Revisit reduce() in js from giving objects in arrays

Revisit reduce() in js from giving objects in arrays

一个新手
Release: 2023-03-16 16:56:01
Original
2469 people have browsed it

Suppose there is such an array:

let person = [
     {id: 0, name: "小明"},
     {id: 1, name: "小张"},
     {id: 2, name: "小李"},
     {id: 3, name: "小孙"},
     {id: 1, name: "小周"},
     {id: 2, name: "小陈"},   
]
Copy after login

We want to remove objects with duplicate IDs in the array, such as two objects with the same ID of 2——

{id: 2, name: "小李"}和{id: 2, name: "小陈"} (去掉任何一个都可以)
Copy after login

How should we do it? ?

In fact, for array objects, traditional deduplication methods are powerless, and iteration methods such as forEach() and filter() are not easy to use; what can really achieve elegant deduplication is the new addition of ES5 A method - reduce()

reduce() method receives a callback function as the first parameter, and the callback function accepts four parameters, namely:

1. cur => The first value or the result of superposition with subsequent values;

2. next => The next value to be superimposed;

##3. index => Index value;

4. arr => The array itself;

The common method of reduce is to continuously superimpose each element in the array. One item and return it;

let log = console.log.bind(console);
let arr = [1,2,3,4,5,6];
arr = arr.reduce((cur,next) => {     return cur + next;
})
log(arr); // 21
Copy after login

It can be seen that the final result of the above code is 1+2+3+4+5+6 = 21;

In addition, reduce can also receive a second parameter, which is used to declare the type and initial value of cur of the callback function (first parameter);

let log = console.log.bind(console);
let arr = [1,2,3,4,5,6];
arr = arr.reduce((cur,next) => {     return cur + next;
},0) //指定cur的类型为Number并且初始值为0,当设为1时,最终打印的值为22log(arr); // 21
Copy after login

reduce() is roughly like this. Through this method, we can very elegantly implement deduplication of objects in the array. Let's go back to the array at the beginning of the article:

let log = console.log.bind(console);
let person = [
     {id: 0, name: "小明"},
     {id: 1, name: "小张"},
     {id: 2, name: "小李"},
     {id: 3, name: "小孙"},
     {id: 1, name: "小周"},
     {id: 2, name: "小陈"},   
];

let obj = {};

person = person.reduce((cur,next) => {
    obj[next.id] ? "" : obj[next.id] = true && cur.push(next);    return cur;
},[]) //设置cur默认类型为数组,并且初始值为空的数组
log(person);
Copy after login
After printing person, we can get the deduplicated array.

The above is the detailed content of Revisit reduce() in js from giving objects in arrays. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template