-2

How do I search all values inside all objects in an array object in javascript?

const array = [
 {
  id: 145,
  name: "Test"
 },
 {
  id: 241,
  name: "Array"
 }
]

if I run a function it returns find('a') it returns the second object or if i run find(1) it returns both of the object

Klt
  • 209
  • 1
  • 3
  • 10

1 Answers1

-3

something like this

var matches = [];
for(i=0;i<array.length;i++)
{
    if(array[i].name.indexOf('a') > -1)
    {
        matches.Push(array[i]);
    }
}
return matches;

This isn't valid javascript as such, but that is how you would write something like this in javascript.

Captain Kenpachi
  • 6,960
  • 7
  • 47
  • 68