W-B28
W-B28

Reputation: 31

Loop over an array of objects return an array

I am trying to return an array of each of the names in the array of objects that satisfy the condition in the if statement. Can you please help me understand why my code is only returning just the first name when the condition is met? My goal is to return an array of all object names that satisfy the condition in the if statement.

function getNamesOfLegalDrivers(people){
  for(let i =0; i < examplePeopleArray.length; i++){
    if(examplePeopleArray[i].age > 15) {
      return examplePeopleArray[i].name
    }
  }
}

const examplePeopleArray = [
  { name: 'John', age : 14},
  { name : 'Joey', age : 16},
  { name : 'Jane', age: 18}
];

console.log(getNamesOfLegalDrivers(examplePeopleArray))

Upvotes: 1

Views: 917

Answers (3)

Jeff Vdovjak
Jeff Vdovjak

Reputation: 1983

It is because you are exiting your function after the first time that the if statement is true. return stops execution of the function and returns the value.

Instead, you need to return all of the values after going through the entire for loop.

function getNamesOfLegalDrivers(people){
  let drivers = [];
  for(let i=0; i < people.length; i++){
    if(people[i].age > 15) {
      drivers.push(people[i]['name']);
    }
  }
    return drivers;

}
const examplePeopleArray = [
  { name: 'John', age : 14},
  { name : 'Joey', age : 16},
  { name : 'Jane', age: 18}
];

console.log(getNamesOfLegalDrivers(examplePeopleArray));

Also, inside your function you are using the global variable examplePeopleArray -- you should be using the people variable (the information passed to the function). Otherwise, if you were to change what array you passed to the function (e.g. a second list of drivers), you would still be looking at the examplePeopleArray.

Upvotes: 2

gregoryBlickel
gregoryBlickel

Reputation: 41

When you start a line with return, it ends the function (similar to how break ends a loop).

function getNamesOfLegalDrivers(people){
  let returnArray = [];
  let numberOfDrivers = people.length;
  for(let i =0; i < numberOfDrivers; i++){
    if(people[i].age > 15) {
      returnArray.push(people[i].name)
    }
  }
   return returnArray;
}

const examplePeopleArray = [
  { name: 'John', age : 14},
  { name : 'Joey', age : 16},
  { name : 'Jane', age: 18}
];

console.log(getNamesOfLegalDrivers(examplePeopleArray))

Upvotes: 4

monogate
monogate

Reputation: 1439

This happens because you return the first occurrence you encounter in this statement:

return examplePeopleArray[i].name

Assuming you want to keep that logic, you should store the desired result in an array and then return the final array outside of the for loop:

function getNamesOfLegalDrivers(people){
let names = [];
  for(let i =0; i < examplePeopleArray.length; i++){
    if(people[i].age > 15) {
      names.push(people[i].name);
    }
  }
  return names;
}

const examplePeopleArray = [
  { name: 'John', age : 14},
  { name : 'Joey', age : 16},
  { name : 'Jane', age: 18}
];

console.log(getNamesOfLegalDrivers(examplePeopleArray))

Also, a cleaner way to achieve the same result would be:

function getNamesOfLegalDrivers(people){
    return people.filter(person => person.age > 15).map(person => person.name);
}

const examplePeopleArray = [
  { name: 'John', age : 14},
  { name : 'Joey', age : 16},
  { name : 'Jane', age: 18}
];

console.log(getNamesOfLegalDrivers(examplePeopleArray))

First, you filter ages higher than 15 and return the result to the map operator which will make sure to return just their names to an array.

Upvotes: 2

Related Questions