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

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

I have an array like

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


        
25条回答
  •  萌比男神i
    2020-11-22 09:03

    I would rather go with regex.

    If your code is as follows,

    vendors = [
        {
          Name: 'Magenic',
          ID: 'ABC'
         },
        {
          Name: 'Microsoft',
          ID: 'DEF'
        }
    ];
    

    I would recommend

    /"Name":"Magenic"/.test(JSON.stringify(vendors))
    

提交回复
热议问题