1. filter() detects numeric elements and returns an array of all elements that meet the conditions.
The filter() method creates a new array, and the elements in the new array check all elements in the specified array that meet the conditions.
Note: filter() will not detect empty arrays.
Note: filter() will not change the original array.
array.filter(function(currentValue,index,arr), thisValue)
Function parameters:
- currentValue must. The value of the current element
- index is optional. The index value of the current element
- arr is optional. The array object to which the current element belongs
- thisValue is optional. Initial value
var arr3 = [1,2,2,3,5,6,3,4]
//通过indexOf返回指定值第一次出现位置的下标,来数组去重
var arr4 = arr3.filter((x,index,self)=>{
return self.indexOf(x) === index
})
console.log(arr4)
2. map() processes each element of the array through the specified function and returns the processed array.
map returns a new array parameter
array.map(function(currentValue,index,arr), thisValue)
Function parameters:
- currentValue must. The value of the current element
- index is optional. The index value of the current element
- arr is optional. The array object to which the current element belongs
- thisValue is optional. Initial value
- The map() method processes the elements sequentially in the order of the original array elements.
Note: map() will not detect empty arrays.
Note: map() will not change the original array.
Note: each value traversed by map() must be returned
var arr = [1, 2, 3,4]
var arr1 = arr.map(x => x *2) //[2, 4, 6,8]
var arr1 = arr.map(x => {
x *2}) //[undefined, undefined, undefined,undefined]
var arr1 = arr.map(x => {
return x *2}) //[2, 4, 6,8]
var arr1 = arr.map(x => {
if (x == 4) {
return x * 2;
}
}) //[undefined, undefined, undefined,8]
var arr1 = arr.map(x => {
if (x == 4) {
return x * 2;
}
}) //[2, 4, 6,8]
3. reduce() calculates the array elements as a value (from left to right).
The reduce() method receives a function as an accumulator, and each value in the array (from left to right) is reduced and finally calculated as a value.
reduce() can be used as a higher-order function to compose the function.
Note: reduce() will not execute the callback function for empty arrays.
array.reduce(function(total , currentValue , currentIndex, arr), initialValue)
Function parameters:
- total is required. Indicates the return value or initial value when the callback was called last time.
- currentValue is required. Current element
- currentIndex is optional. Index of current element
- arr is optional. The number of the current element is optional.
- initialValue The initial value group object passed to the function.
//求和
var nums1 = nums.reduce((num, cur) => {
//因未先传入值,num为第一个值15,cur为第二个值2
return num + cur
})
//数组去重
var nums2 = nums.reduce((num, cur, index, arr) => {
//因未先传入值,num为第一个值15,cur为第二个值2
num.indexOf(cur) === -1 && num.push(cur)
return num
}, [])
//数组去重s
var nums3 = nums.reduce((num, cur) => {
//因未先传入值,num为第一个值15,cur为第二个值2
return num.includes(cur) ? num : num.concat(cur)
}, [])