Tim Jansen Tim Jansen - 2 months ago 9
HTML Question

Search through Object Array in Javascript

I am having an javascript array with objects in it:

var array;
array[{name: "Peter",class: "7"},{name: "Klaus",class: "3"}];


How to find out if in this array is the name Peter?

EDIT: I want to have something like this

if (array.find("Peter"))
{
...
}

Answer

Use find.

arr.find(row => row.name === 'Peter');

Don't forget to use the polyfill in the link if your targeted browsers don't support this method.