Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a value exists in an Array object in JavaScript or Angular

I want to check if a value exist in an array object. Example:

I have this array:

[
    {id: 1, name: 'foo'},
    {id: 2, name: 'bar'},
    {id: 3, name: 'test'}
]

And I want check if id = 2 exists here.

like image 358
Stiven Castillo Avatar asked Nov 17 '17 15:11

Stiven Castillo


1 Answers

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);
like image 188
Karim Avatar answered Oct 20 '22 05:10

Karim