js array object filtering -- filter,find,some,every

1. The filter() method creates a new array. The elements in the new array are all qualified elements in the specified array.
Original array unchanged
Empty arrays are not detected

let arr1 = [1,2,3,4]
let arr2 = arr1.filter(item=>item===1)
console.log(arr1, 'arr1')  // [1,2,3,4] arr1
console.log(arr2, 'arr2') // [1] arr2
let arr3 = [{
    desc: 'aaaa'
     name: 'aa'
let arr4 = arr3.filter(item=>item.name === 'aa')
console.log(arr4, 'arr4') // [{id:1,name:'aa', desc:'aaaa'},{id:3,name:'aa'}] arr4

For an empty array, the function will not execute.
Do not change the original array
Returns the value of the first array element that meets the test conditions

let arr5 = [1,2,1,3,4,5]
let arr6 = arr5.find(item=>item===1)
console.log(arr6, 'arr6')  // 1 arr6

let arr7 = arr3.find(item=>item.name === 'aa')
console.log(arr7, 'arr7') // {id:1,name:'aa',desc:'aaaa'} arr7

3. some is used to check whether the elements in the array meet the specified conditions
Each element of the array is executed in turn - if one element satisfies the condition (that is, as long as the condition is satisfied, it is equal to or), the expression returns true, and the remaining elements are not detected again
----------------------Returns false if there are no elements that meet the criteria

let someArr1 = [1,2,3,4]
      let someArr2 = someArr1.some(item=>item === 1)
      console.log(someArr2, 'someArr1') // true  someArr1
      let someArr4 = [{
      let someArr3 = someArr4.find(info=>{
          return arr3.some(item=>item.id === info.id)
      console.log(someArr3) // {id:1,name:'bb'}

4. The every() method is used to check whether all elements of the array meet the specified conditions (provided by the function)
every() method uses the specified function to detect all elements in the array - if one element in the array is detected to be unsatisfied, the entire expression returns false, and the remaining elements will not be detected again
------------------------------------------Returns true if all elements meet the criteria

let everyArr = [1,2,3,4]
let everyArr2 = everyArr.every(item=>item===1)
console.log(everyArr2, 'everyArr2') //false "everyArr2"

Tags: Javascript

Posted on Tue, 03 Dec 2019 17:37:03 -0800 by Citizen