0

I am having trouble with some basic javascript. I want this function to return an array of all objects within the given array that have the name "Ray" assigned to name. I can't get the push part to work.

  const people = [{name: "Jack", age: 30}, {name: "Ray", age: 32}, {name: "Anna", age: 28}]; 
  
function findRay(arr) {
  let response = []; 
  for(let i = 0; i < arr.length; i++) {
    if(arr[i].name === "Ray") {
      response.push(arr[i]); 
    }
  }
  return response;
}
  
console.log(findRay(people));  
P.S.
  • 15,970
  • 14
  • 62
  • 86
J Seabolt
  • 2,576
  • 5
  • 25
  • 57

1 Answers1

2

While not exactly what you were looking for, this is a good use case for filter(). So you could do something like const findRay = arr => arr.filter(person => person.name === "Ray").

Dan Mandel
  • 637
  • 1
  • 8
  • 26