You can use Array.prototype.filter
method to search for specific values in an array of objects. Here is an example:
Suppose we have an array of objects, cars:
index.tsx220 chars7 lines
We can use filter method to search for cars with 'Honda' make:
index.tsx62 chars2 lines
hondaCars
will be an array with two cars objects with 'Honda' make:
index.tsx103 chars5 lines
You can also search for values in specific keys of an object using Object.keys
method and Array.prototype.includes
. Here is an example:
index.tsx201 chars11 lines
In the above example, Object.keys(car)
will return an array ['make', 'model', 'year']
, and hasSearchValue
will evaluate to true because 'Corolla'
is in the array.
gistlibby LogSnag