0

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))
1
  • just use array.filter and to get names, use array.map, ex: examplePeopleArray.filter(p => p.age >=16).map(p => p.name); Commented Nov 12, 2019 at 21:28

3 Answers 3

3

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);
Sign up to request clarification or add additional context in comments.

Comments

0

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);

Comments

0

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.

1 Comment

Oh! that was a typo.Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.