PrefaceIn actual projects, the most common processing may be in calculation and loop logic. You can use the reduce method in the array to solve many problems and make your code style more elegant! reduce syntax
Parameter DescriptionThe reducer function needs to receive 4 parameters: callback
initialValue
Return Value
Some common reduce methodsThe sum of all elements in the array const arr = [1, 2, 3, 4]; const result = arr.reduce((acc, cur) => acc + cur) console.log(result) // 10 Count the number of times each element occurs in an array const nums = ['1', '1', '1', '2', '3']; const countednums = nums.reduce((acc, cur) => { if (cur in acc) { acc[cur]++; } else { acc[cur] = 1; } return acc; }, {}); console.log(countednums); // {1: 3, 2: 1, 3: 1} Flattening an array const arr = [['a', 'b'], ['b', 'c'], ['d', 'e']] const flatten = arr => { return arr.reduce((acc, cur) => { return acc.concat(cur) }, []) } console.log(flatten(arr)); // ["a", "b", "b", "c", "d", "e"] Array deduplication const arr = [22,341,124,54,4,21,4,4,1,4,4]; const result = arr.sort().reduce((acc, cur) => { if(acc.length === 0 || acc[acc.length-1] !== cur) { acc.push(cur); } return acc; }, []); console.log(result); // [1, 124, 21, 22, 341, 4, 54] Find the maximum value in an array const arr = [1, 2, 3, 5, 1] let result = arr.reduce((acc, cur) => Math.max(acc, cur)) console.log(result) Promises are called in order This method actually processes the value of the promise, and processes the value of the previous promise as the value of the next promise. const prom1 = a => { return new Promise((resolve => { resolve(a) })) } const prom2 = a => { return new Promise((resolve => { resolve(a * 2) })) } const prom3 = a => { return new Promise((resolve => { resolve(a * 3) })) } const arr = [prom1, prom2, prom3] const result = arr.reduce((all, current) => { return all.then(current) }, Promise.resolve(10)) result.then(res => { console.log(res); }) at lastThis article shares some reduce processing methods that are commonly used in daily development. You can use them directly in your project or repackage them. This is the end of this article about how to use the reduce method in js to make your code more elegant. For more relevant content about the js reduce method to make the code elegant, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Installation and configuration method of Zabbix Agent on Linux platform
>>: Detailed explanation of MySQL Strict Mode knowledge points
Table of contents Preface Child components pass d...
Table of contents Why choose react-beautiful-dnd ...
Linux version upgrade: 1. First, confirm that the...
The commands pulled by docker are stored in the /...
Table of contents 1. Preprocessing 2. Compilation...
Table of contents JSON.parse JSON.parse Syntax re...
Next, I will install Java+Tomcat on Centos7. Ther...
This article shares with you how to query the sta...
1. Link layout of the new site homepage 1. The loc...
The following content introduces the process and ...
Install TomCat on Windows This article will intro...
Written at the beginning I remember seeing a shar...
Ordered List XML/HTML CodeCopy content to clipboa...
Statistics of QPS values in the last N seconds ...
Recently, the company has put forward a requireme...