If there is a need to search across all object values in an array in JavaScript, a combination of filter
, entries
, and some
can be used.
How to search across object values
In a frontend data table, rows may contain information about people:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
| const people = [
{
"id": 5,
"firstName": "Buzz",
"lastName": "Lightyear2"
},
{
"id": 2,
"firstName": "Buzz",
"lastName": "Lightyear"
},
{
"id": 1,
"firstName": "Woody",
"lastName": "Pride"
},
{
"id": 3,
"firstName": "Potato",
"lastName": "Head"
},
{
"id": 4,
"firstName": "Slinky",
"lastName": "Dog"
}
];
|
To provide a “search” across all of the values, use the following:
1
2
3
| const query = "Slinky".toLowerCase();
const results = people.filter(o => Object.entries(o).some(e => String(e[1]).toLowerCase().includes(query)));
console.log(results); // [ { id: 4, firstName: 'Slinky', lastName: 'Dog' } ]
|
If id
or other fields are not preferable in the search, use the rest
operator.
1
2
3
4
5
6
| const query = "Slinky".toLowerCase();
const results = people.filter(o => {
const { id, ...rest } = o;
return Object.entries(rest).some(e => String(e[1]).toLowerCase().includes(query))
});
console.log(results); // [ { id: 4, firstName: 'Slinky', lastName: 'Dog' } ]
|