filter
Filter returns a new array, not an empty array
1 let arr = [1,2,3,4,5,6,7,8,9,10];
2 let new_arr = arr.filter((n) => { console.log(n) ; return n > 5})
3 console.log(new_arr,arr)
Result: new_arr = [6, 7, 8, 9, 10]
The arrow function returns false or true each time, if true, the array is added, false does not add to the new array.
map
The map() method returns a new array whose elements are the values processed by the call to the original array element
Generates a new array, and does not operate on an empty array
let map_arr = new_arr.map(n => n * 2)
Each time the array is multiplied by two, the result return is added to the new array, because the return value is in a row, so you can return {} and return directly.
reduce
The reduce() method takes a function as an accumulator, and each value in the array (from left to right) starts to shrink and eventually evaluates to a value. The idea is to loop through each value, perform an operation, and return the result to the next loop.
Reduce () does not perform callbacks on empty arrays.
let map_arr = [12, 14, 16, 18, 20] // callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: number[]) => number): let reduce_sum = map_arr.reduce((preValue, curValue, curIndex) => { console.log(curIndex) // return preValue + curValue + curIndex return preValue + curValue }, 0) console.log(reduce_sum)