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

后端 未结 25 1613
借酒劲吻你
借酒劲吻你 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

提交回复
热议问题