2

I have below array how to get list of names where the age is greater than 18. so the out put should be: ["Anna", "Bob"]

  friends = [{
  name: 'Anna',
  books: ['Bible', 'Harry Potter'],
  age: 21
}, {
  name: 'Bob',
  books: ['War and peace', 'Romeo and Juliet'],
  age: 26
}, {
  name: 'Alice',
  books: ['The Lord of the Rings', 'The Shining'],
  age: 18
}]

I have tried below

let names = friends.map((item) => {if (item.age > 18){ return item.name}});

but i got below output

["Anna", "Bob", undefined]

4 Answers 4

3

Use Array.filter() before Array.map() since map always returns a value and you'll get undefined if you don't specify return statement. For 3-elements array there's always going to be 3-elements result.

let friends = [{
  name: 'Anna',
  books: ['Bible', 'Harry Potter'],
  age: 21
}, {
  name: 'Bob',
  books: ['War and peace', 'Romeo and Juliet'],
  age: 26
}, {
  name: 'Alice',
  books: ['The Lord of the Rings', 'The Shining'],
  age: 18
}]

let result = friends.filter(f => f.age > 18).map(f => f.name);
console.log(result);

Sign up to request clarification or add additional context in comments.

Comments

2

You can use Array.prototype.reduce

let friends = [{
  name: 'Anna',
  books: ['Bible', 'Harry Potter'],
  age: 21
}, {
  name: 'Bob',
  books: ['War and peace', 'Romeo and Juliet'],
  age: 26
}, {
  name: 'Alice',
  books: ['The Lord of the Rings', 'The Shining'],
  age: 18
}];

let ans = friends.reduce((acc, val) => (val.age > 18 && acc.push(val.name),acc), []);

console.log(ans);

1 Comment

Here is a little tip, instead of your tenary that's returning a useless "", you could use a logical and, &&.. eg.. val.age > 18 && acc.push(val.name);
1

You are getting undefined as the last item in names array because map function is used to transform each item in the array it is called on and then return each transformed value.

If you call it on an array of length 3, map function will return an array of same length. Since you are only returning those names from map function where age is greater than 18, last object where age is not greater than 18, you are not transforming it and returning its name property, so you get undefined.

One way to achieve the desired result is to use filter function to filter out the object where age is not greater than 18 and then call map function on that filtered array.

In above approach, you code will first iterate over the friends array and then iterate over the filtered array.

You can achieve desired result and iterate over friends array only once using reduce function

const friends = [{
  name: 'Anna',
  books: ['Bible', 'Harry Potter'],
  age: 21
}, {
  name: 'Bob',
  books: ['War and peace', 'Romeo and Juliet'],
  age: 26
}, {
  name: 'Alice',
  books: ['The Lord of the Rings', 'The Shining'],
  age: 18
}];

const res = friends.reduce((acc, curr) => (curr.age > 18 && acc.push(curr.name), acc), []);

console.log(res);

Comments

0
let friends = [{   name: 'Anna',   books: ['Bible', 'Harry Potter'],   age: 21 }, {   name: 'Bob',   books: ['War and peace', 'Romeo and Juliet'],   age: 26 }, {   name: 'Alice',   books: ['The Lord of the Rings', 'The Shining'],   age: 18 }]

let ages = friends.filter((friends) => friends.age>18)

let finalResult = ages.map(fun => ages.name)

console.log(finalResult)

Use Array.filter()

Comments

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.