JavaScript Get array of properties from Array of Objects in Java script / Type script array from properties file,array properties in javascript,array properties

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]
Answer:1

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);
Answer:2

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);
Answer:3

When I click more button my div 1 is replacing div 2. Most of the time div2 contains lots of text, and requires users to scroll down in order to read the entire post. The problem is that by clicking ...

When I click more button my div 1 is replacing div 2. Most of the time div2 contains lots of text, and requires users to scroll down in order to read the entire post. The problem is that by clicking ...

I have some squares sliding down the page, in a chained promise fashion: https://jsfiddle.net/u4x0qwfo/3 The code is: new Promise(function(resolve, reject) { $("#shape").css({ top: 100 }); ...

I have some squares sliding down the page, in a chained promise fashion: https://jsfiddle.net/u4x0qwfo/3 The code is: new Promise(function(resolve, reject) { $("#shape").css({ top: 100 }); ...

  1. javascript promise returned is ignored
  2. javascript promise returned
  3. js promise returned is ignored
  4. js return promise
  5. javascript handle returned promise

I have many dinamic tables, each one creates a dinamic master checkbox inside an input and several checkboxes inside td's (each one has a different ID). The idea is once I check or uncheck the master ...

I have many dinamic tables, each one creates a dinamic master checkbox inside an input and several checkboxes inside td's (each one has a different ID). The idea is once I check or uncheck the master ...

  1. check all checkboxes inside div jquery
  2. check all checkboxes inside a div

I'm having a bit of a trouble here. I'm using RequireJS to load my modules and dependencies. I am trying to avoid polluting the global namespace, so I am using the following configuration as a ...

I'm having a bit of a trouble here. I'm using RequireJS to load my modules and dependencies. I am trying to avoid polluting the global namespace, so I am using the following configuration as a ...