Common methods of JS array objects filter, find, some, every, map

filter

The filter method will not change the original array , but will create a new array, and each element in the new array is all eligible

elements without checking for empty arrays.

let arr1 = [1,2,1,3]
let arr2 = arr1.filter(item=>item>1)
console.log(arr1,arr2)//[1, 2, 1, 3]  [2, 3]

find

The find method will not change the original array, and returns the first array element that meets the conditions. For an empty array, it will not detect an empty array.

let arr1 = [1,2,1,3]
let arr2 = arr1.find(item=>item>1)
console.log(arr1,arr2)//[1, 2, 1, 3] 2

some

The some method is used to detect whether the elements in the data meet the specified conditions. Each element of the array will be executed in turn. If one element meets the condition, the expression will return true, and the remaining elements will not be tested again. If there is no element that meets the condition element, returns false

let arr1 = [1,2,1,3]
let arr2 = arr1.some(item=>item>4)
console.log(arr1,arr2)//false

every

every is used to detect whether all elements of the array meet the specified conditions. If one element does not meet the specified conditions, it returns false, and the remaining elements will not be tested again. If all elements meet the conditions, it returns true

let arr1 = [1,2,1,3]
let arr2 = arr1.every(item=>item>4)
console.log(arr1,arr2)//false

map

The map method calculates each element of the array to get a new array

let arr1 = [1,2,1,3]
let arr2 = arr1.map(item=>item+=1)
console.log(arr1,arr2)//[1, 2, 1, 3]  [2, 3, 2, 4]

Guess you like

Origin blog.csdn.net/weixin_45294459/article/details/129545140