array.select() in javascript

前端 未结 5 2070
时光取名叫无心
时光取名叫无心 2021-01-31 06:44

Does javascript has similar functionality as Ruby has?

array.select {|x| x > 3}

Something like:

array.select(function(x) { i         


        
5条回答
  •  粉色の甜心
    2021-01-31 07:21

    There's also Array.find() in ES6 which returns the first matching element it finds.

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find

    const myArray = [1, 2, 3]
    
    const myElement = myArray.find((element) => element === 2)
    
    console.log(myElement)
    // => 2
    

提交回复
热议问题