Jquery how to find an Object by attribute in an Array

前端 未结 11 2107
灰色年华
灰色年华 2020-12-02 11:55

Given I have an array of \"purpose\" objects:

//array of purpose objects:
var purposeObjects = [
    {purpose: \"daily\"},
    {purpose: \"weekly\"},
    {pu         


        
11条回答
  •  时光取名叫无心
    2020-12-02 12:41

    Javascript has a function just for that: Array.prototype.find. As example

    function isBigEnough(element) {
      return element >= 15;
    }
    
    [12, 5, 8, 130, 44].find(isBigEnough); // 130
    

    It not difficult to extends the callback to a function. However this is not compatible with IE (and partially with Edge). For a full list look at the Browser Compatibility

提交回复
热议问题