How to determine if Javascript array contains an object with an attribute that equals a given value?

后端 未结 25 1163
借酒劲吻你
借酒劲吻你 2020-11-22 08:17

I have an array like

vendors = [{
    Name: \'Magenic\',
    ID: \'ABC\'
  },
  {
    Name: \'Microsoft\',
    ID: \'DEF\'
  } // and so on... 
];


        
相关标签:
25条回答
  • 2020-11-22 09:10

    if you're using jquery you can take advantage of grep to create array with all matching objects:

    var results = $.grep(vendors, function (e) {
        return e.Name == "Magenic";
    });
    

    and then use the results array:

    for (var i=0, l=results.length; i<l; i++) {
        console.log(results[i].ID);
    }
    
    0 讨论(0)
提交回复
热议问题