Find matched object in es6

How to get matched id object or other value? 

const arr = [{'id': 1, ‘name’: ‘abc’'},{'id': 5,  ‘name':'xyz'}];

const result = arr.find((v) => v.id === 5);

if we want an array of matching elements:

const result = arr.filter((v) => v.id === 5);

The find() method returns the first value in the array, if an element in the array satisfies the provided testing function. Otherwise return undefined.



Comments

Popular posts from this blog

world tourism guide Template

Es6 how to check if value is not null or undefined?