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

 1, filter () method creates a new array of new elements in the array by checking the specified array qualifying all the elements.
     Original array unchanged
     will not be an empty array detection

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 = [{
    id:1,
    name:'aa',
    desc: "aaaa"
 },{
    id:2,
    name:'bb'
  },{
     id:3,
     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

 2, find () for empty array, the function is not executed.
 It does not change the original array
 returns the first element of the array in line with the value of 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 elements in the array for detecting whether the specified conditions are satisfied
will be followed by the implementation of each element of the array - if there is an element satisfying the condition (i.e., as long as the conditions can be satisfied, or equivalent), the expression returns true, the remaining elements will not perform the detection
---------------------- element if the condition is not satisfied, then return false

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

4, every () method for detecting whether all elements of the array are meet the specified criteria (provided by the function)
 Every () method uses all of the elements specified function detection array - the array is detected if one element is not satisfied, the expression of the entire formula returns false, and the remaining elements will not be detected
------------------------------------- ----- If all elements satisfy the conditions are true

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

 

Guess you like

Origin www.cnblogs.com/layaling/p/11978503.html