JavaScript Advanced~Array Method reduce

reduce()   Method performs a reducer function (ascending execution) on each element in the array, summarizing its results as a single return value.

A little abstract, meaning you can see that after each element has been executed, there will be a summary result from which you can return a value you want, which can be of any type.

Grammar Format:

arr.reduce(callback(accumulator, currentValue[, index[, array]])[, initialValue])

The clear point is:

arr.reduce(function(accumulator, currentValue, currentIndex, array), initialValue)

There are two parameters to receive:

Callback: callback function

initialValue: Initial value

Initial value is optional

Let's look at the parameters of the callback function, which takes four parameters:

Accumulator (acc) (accumulator)

Current Value (cur) (current value)

Current Index (idx) (current index, optional)

Source Array (src) (source array, optional)

There are two cases of accumulator and current value

When the callback function is first executed, there are two situations in which the accumulator and the current value are taken: if initialValue is provided when reduce() is called, the accumulator is taken as initialValue, and the current value is taken as the first value in the array; If not provided   InitialValue, then the accumulator takes the first value in the array and the current value takes the second value in the array.

Note: If initialValue is not provided, reduce will execute the callback method from index 1, skipping the first index. If initialValue is provided, start at index 0.

reduce()   How to run

[0, 1, 2, 3, 4].reduce(function(accumulator, currentValue, currentIndex, array){
  return accumulator + currentValue;
});

Execution process:

callback accumulator Current Value Current Index Current Array Return results
First Execution 0 1 1 [0, 1, 2, 3, 4] 1
Second Execution 1 2 2 [0, 1, 2, 3, 4] 3
Third Execution 3 3 3 [0, 1, 2, 3, 4] 6
Fourth Execution 6 4 4 [0, 1, 2, 3, 4] 10

As you can see from the table above, the result of each execution will be the value of the next accumulator.

Note: When no initial value is specified, execution begins at index 1

let acc2=arr.reduce(function(accumulator, currentValue, currentIndex, array){
		return accumulator+currentValue
	},10)
callback accumulator Current Value Current Index Current Array Return results
First Execution 10 0 0 [0, 1, 2, 3, 4] 10
Second Execution 10 1 1 [0, 1, 2, 3, 4] 11
Third Execution 11 2 2 [0, 1, 2, 3, 4] 13
Fourth Execution 13 3 3 [0, 1, 2, 3, 4] 16
Fifth Execution 16 4 4 [0, 1, 2, 3, 4] 20

Example 1: Array Unweighting

let arr3 =['q','q','w','e','f','d','s','w','e','w','e','d','s','a','a','a','s']
	let res=arr3.reduce(function(accumulator, currentValue){
		if(accumulator.indexOf(currentValue)===-1){
			accumulator.push(currentValue)
		}
		return accumulator
	},[])

Example 2: Counting the number of character accidents

let str ='qqqqwwwwssssddddcccfffvvvvvbbbbb'
	let arr4=str.split("")
	let map2=arr4.reduce(function (accumulator, currentValue) {
		if(accumulator.get(currentValue)===undefined){
			accumulator.set(currentValue,1)
		}else{
			accumulator.set(currentValue,accumulator.get(currentValue)+1)
		}
		return accumulator
	},new Map())

Or:

let str3 ='qqqqwwwwssssddddcccfffvvvvvbbbbb'
	let arr6=str.split("")
	let arr7=arr4.reduce(function (accumulator, currentValue) {
		if(currentValue in accumulator){
			accumulator[currentValue]++
		}else{
			accumulator[currentValue]=1	
		}
		return accumulator
	},{})

 

Posted by etnastyles on Mon, 06 Dec 2021 10:26:41 -0800