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

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

    You cannot without looking into the object really.

    You probably should change your structure a little, like

    vendors = {
        Magenic:   'ABC',
        Microsoft: 'DEF'
    };
    

    Then you can just use it like a lookup-hash.

    vendors['Microsoft']; // 'DEF'
    vendors['Apple']; // undefined
    

提交回复
热议问题