Reputation: 21
I'm trying to to return a new array of names of the people who are 16 or older.
const examplePeopleArray = [
{ name: 'John', age: 14 },
{ name: 'Joey', age: 16 },
{ name: 'Jane', age: 18 }
];
function getNamesOfLegalDrivers(people) {
let legalDriver = []
for (let i = 0; i > People.length; i++){
if ( i >= 1){
legalDriver += people.name.push(i)
} else {
legalDriver += i
}
}
return legalDriver;
}
console.log(getNamesOfLegalDrivers(examplePeopleArray))
Upvotes: 0
Views: 54
Reputation: 373
The problem with your code is that this line:
for (let i = 0; i > People.length; i++){
Should be:
for (let i = 0; i > people.length; i++){
(Lower case "people")
But as another user commented, using the .filter()
method would be much simpler:
examplePeopleArray.filter(person => person.age >= 16)
That returns a new array with all items in the examplePeopleArray
for which person.age >= 16
returns true.
Upvotes: 0
Reputation: 386560
Just filter and map the names.
const
array = [{ name: 'John', age: 14 }, { name: 'Joey', age: 16 }, { name: 'Jane', age: 18 }],
result = array
.filter(({ age }) => age >= 16)
.map(({ name }) => name)
console.log(result);
Upvotes: 0
Reputation: 842
Just use array.filter and array.map, here is the snippet:
const examplePeopleArray = [
{ name: 'John', age: 14 },
{ name: 'Joey', age: 16 },
{ name: 'Jane', age: 18 }
];
let legalDriverNames = examplePeopleArray.filter(p => p.age >=16).map(p => p.name);
console.log(legalDriverNames);
Upvotes: 3