Check if value exists in Array object Javascript or Angular

后端 未结 4 984
粉色の甜心
粉色の甜心 2021-01-06 15:43

I want to check if value exist in array object, example:

I have this array:

[
    {id: 1, name: \'foo\'},
    {id: 2, name: \'bar\'},
    {id: 3, nam         


        
4条回答
  •  情书的邮戳
    2021-01-06 16:18

    You can use Array.prototype.some

    var a = [
       {id: 1, name: 'foo'},
       {id: 2, name: 'bar'},
       {id: 3, name: 'test'}
    ];        
    
    var isPresent = a.some(function(el){ return el.id === 2});
    console.log(isPresent);

提交回复
热议问题