Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to return only the names of the people from an array born between certain ages with a function

This is the code I’m currently working with. For an example "findCodersBetween(coders, 1960, 1986)" would return "[{name: ‘Alice’, born: 1990}, {name: ‘Susan’, born: 1960}, {name: ‘Charlotte’, born: 1986}]". But what I want is for the function to only output the names "[‘Alice’, ‘Susan’, ‘Charlotte’]". I think the answer is simple I just can’t figure it out.

const allCoders = [
  {name: 'Ada', born: 1816},
  {name: 'Alice', born: 1990},
  {name: 'Susan', born: 1960},
  {name: 'Eileen', born: 1936},
  {name: 'Zoe', born: 1995},
  {name: 'Charlotte', born: 1986},
]

const findCodersBetween = (coders, startYear, endYear) => {
  return coders.filter((coder) => coder.born >= startYear && coder.born <= endYear);
};

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You can use map and return only the name: .map(coder => coder.name)

const allCoders = [
  {name: 'Ada', born: 1816},
  {name: 'Alice', born: 1990},
  {name: 'Susan', born: 1960},
  {name: 'Eileen', born: 1936},
  {name: 'Zoe', born: 1995},
  {name: 'Charlotte', born: 1986},
]

const findCodersBetween = (coders, startYear, endYear) => {
  return coders.filter((coder) => coder.born >= startYear && coder.born <= endYear).map(coder => coder.name);
};

console.log(findCodersBetween(allCoders, 1960, 1986))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading