filter

Creates a new array of filtered elements that match the specified criteria.

Features: (1) Filter will traverse each item in the number group, find the element that meets the specified condition and return. Returns a new array. (2) Filter will not detect the empty array and will not change the original array.Copy the code
let arr = [1.2.6.7.8];

let filterArr = arr.filter(item= >{return item == 6});

console.log('arr:',arr); // [1, 2, 6, 7, 8],
console.log('filterArr:',filterArr); / / [6]


// Empty array not detected, not printed Kong
var newArr = [].filter(item= >{console.log('Kong')});

console.log(newArr); / / []
Copy the code

some

Checks whether an element in an array meets a specified condition.

(1) Return true if one element matches the condition, and subsequent elements are not checked. (2) The empty array will not be detected and the original array will not be changedCopy the code
let hadVal2 = arr.some(item= >{
    console.log(item); / / 1, 2
    return item == 2;
});

let hadVal10 = arr.some(item= >{return item == 10});

console.log('arr:',arr); // [1, 2]
console.log('hadVal2:',hadVal2); // true
console.log('hadVal10:',hadVal10); // false 
Copy the code

every

Checks that every element in an array (all elements) matches the specified condition.

(1) Return false if one of the following elements is not executed. (2) The empty array will not be detected and the original array will not be changedCopy the code
// Check whether each element in the array is less than 5 after the addition of 2
let everyFlag = arr.every(item= >{
    console.log(item); / / 1,2,6
    return item + 2 < 5
});

console.log('arr:',arr); // [1, 2, 6, 7, 8]
console.log('everyFlag:',everyFlag); // false

let everyEmptyFlag = [].every(item= >{console.log('Kong... '); });console.log('everyEmptyFlag:',everyEmptyFlag); // true

Copy the code