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

后端 未结 25 1260
借酒劲吻你
借酒劲吻你 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 08:46

    Correct me if i'm wrong.. i could have used forEach method like this,

    var found=false;
    vendors.forEach(function(item){
       if(item.name === "name"){
           found=true;
    
       }
    });
    

    Nowadays i'm used to it ,because of it simplicity and self explanatory word. Thank you.

提交回复
热议问题