“This is the sixth day of my participation in the First Challenge 2022. For details: First Challenge 2022”
Hello, everyone, I touch fish small public, the real strong, will not complain, if you do not want to be looked down upon by others, you have to pay more than others ten times a hundred times more effort, to stand higher than others. The last article was a collection of commonly used regular expressions that implement the year-round calendar display and set up holidays/weekdays. Today we’ll look at 11 uses of Reduce (three basic and eight advanced methods). To be honest, I always feel that I understand the following code, but MY presentation is very poor, always afraid that you will not understand my presentation, what is not good, I hope you give more advice.
Knowledge of reduce
Reduce () receives a function as an accumulator, and each value in the array (from left to right) begins to shrink and eventually evaluates to a single value.
Reduce () can be used as a higher-order function for compose of functions.
Note: Reduce () does not perform a callback for an empty array.
Syntax: array.reduce(function(Total, currentValue, currentIndex, ARR), initialValue)
Parameters:
Basic Edition (most commonly used)
We first initialize an array
Let arr1 =,6,8,9,3,6 [1]Copy the code
(1) the sum of the array items summation and multiplication
// let arr2= arr1.reduce((a,b)=>{return a+b}) console.log(arr2,"arr1arr1"); //33 // multiply let arr2= arr1.reduce((a,b)=>{return a*b}) console.log(arr2,"arr1arr1"); / / 7776Copy the code
(2) Find the maximum value between the array items
let arr2= arr1.reduce((a,b)=>{ return Math.max(a,b) }) console.log(arr2,"arr1arr1"); / / 9Copy the code
(3) Array deduplication
var newArr = arr1.reduce(function (a, b) { a.indexOf(b) === -1 && a.push(b); return a; } []); // here [], the initial value passed to the function console.log(newArr,"newArr"); //[1, 6, 8, 9, 3]Copy the code
premium
(4) Array flat
let arr1 = [[1, 6, 8], [9, 3, 6]];
var newArr = arr1.reduce(function (a, b) {
return a.concat(b);
}, []);
console.log(newArr,"newArr");//[1, 6, 8, 9, 3, 6]
Copy the code
(5) Array to object
const people = [ { area: "shenZhen", name: "YXL", age: 19 }, { area: "guangZhou", name: "XXL", age: 24 } ]; Const obj = people.reduce((a, b) => {const {age,... rest } = b; a[age] = rest; return a; }, {}); console.log(obj,"obj"); // {19: {area: "shenZhen", name: "YXL"},24: {area: "guangZhou", name: "XXL"}}Copy the code
(6) Array segmentation
Function fenGe(arr = [], size = 1) {return arr.length? arr.reduce( (a, b) => ( a[a.length - 1].length === size ? a.push([b]): a[a.length - 1].push(b),a), [[]] ) : []; } const arr = [1, 2, 3, 4, 5]; fenGe(arr, 2); // [1, 2], [3, 4], [5]]Copy the code
(7) The number of array elements
let nums = ["2", "4", "5", "5", "2","1"]; Let arr = nums.reduce((a, b) => {if (b in a) {//if (b in a) a does not have b attribute a[b]++; } else { a[b] = 1; // Add a b attribute to a and assign 1} return a; }, {}); //reduce(), ES6 array merge method, where the initial value is set to an empty object console.log(arr); //{1: 1, 2: 2, 4: 1, 5: 2}Copy the code
(8) Location record of each element in array (subscript of array element)
Function Position(arr = [], val) {function Position(arr = [], val) {function Position(arr = [], val) { V: current element; Return arr. Reduce ((t, v, I) => (v === val && t.ush (I), t), []); } const arr = [1, 1, 4, 2, 1, 6, 6]; Position(arr, 1); / / [0, 1, 4]Copy the code
(9) Grouping of attributes of each element of the array
Function Group(arr = [], key) {//reduce(), ES6; When the object has no corresponding key(area of the current element), the object adds the key attribute to the object and initializes an empty array. If the object has the key attribute, push the current element return key? arr.reduce((t, v) => (! t[v[key]] && (t[v[key]] = []), t[v[key]].push(v), t), {}) : {}; } const arr = [ { area: "a", name: "Amy", age: 27 }, { area: "b", name: "Tom", age: 25 }, { area: "a", name: "Anna", age: 23 }, { area: "c", name: "Lida", age: 21 }, { area: "a", name: "Troy", age: 19 } ]; Group(arr, "area"); // { GZ: Array(2), SZ: Array(2), FS: Array(1) }Copy the code
The printed value of this object
(10) Return the specified key value of the object
Function GetKeys (obj = {}, keys = []) {/ / Object. The keys (obj) / / [" a ", "b", "c", "d"] / / reduce (), ES6 array method, here is the initial value is an empty Object; Return object.keys (obj).reduce((t, v) => (keys.includes(v) && (t[v] = obj[v]), t), {}); } const target = { a: 1, b: 2, c: 3, d: 4 }; const keyword = ["b", "c"]; GetKeys(target, keyword); // { b: 2, c: 3 }Copy the code
(11) Array filtering
Function arrFilter(arr1 = [], arr2 = []) {//reduce(); Return arr1.reduce((t, v) => (! arr2.includes(v) && t.push(v), t), []); ,3,5,7,8,9} const arr1 = [1] const arr2 =,5,8,9 [1] arrFilter (arr1, arr2) / / [3, 7)Copy the code
conclusion
Well, this is the end of the article, welcome everyone (like + comment + attention) questions can come to exchange; I hope you find this article useful and support me. Today is the sixth day of my participation in the first Wenwen Challenge 2022, come on!