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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With