Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

128
Views
Trying to return a filter() value from an array of objects based on conditionals

I'm trying to return only a part of my object as an array based on conditionals but I'm kinda stumped.

I have an array of objects and I want to return an array of names for each key : value they fit in.

I do get only the unique pairings but I'm returning the food key:value as well and all of it is still inside an object not a new array. Some insight would be greatly appreciated. Newer to coding.

const organizeNames = function (foods) {
 let foodNames = foods.filter((names) => {
  if (names.food === 'oranges') {
  return names.name;
 }
});

console.log(foodNames);
};
console.log(
  organizeNames([
   { name: 'Samuel', food: 'oranges' },
   { name: 'Victoria', food: 'pizza' },
   { name: 'Karim', food: 'pizza' },
   { name: 'Donald', food: 'pizza' },
  ])
 );

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You're really close here. What you need to incorporate is .map() to map your list of objects to a list of names. Your filter part works, partly by accident, so I've fixed it to be more correct (return names.food === 'oranges'), and then once you have your list of objects that match 'oranges' for their food, you map that filtered list into a list of names by doing .map(names => names.name)

const organizeNames = function (foods) {
  let foodNames = foods.filter((names) => {
    // only keep items whose .food property === 'oranges'
    return names.food === 'oranges'; // returns true or false
  }).map(names => {
    // map the filtered list of objects into a list of names by
    // returning just the object's .name property
    return names.name;
  });

  return foodNames;
};
console.log(
  organizeNames([
   { name: 'Samuel', food: 'oranges' },
   { name: 'Victoria', food: 'pizza' },
   { name: 'Karim', food: 'pizza' },
   { name: 'Donald', food: 'pizza' },
  ])
);

about 4 years ago · Juan Pablo Isaza Report

0

The filter() callback function should just return true or false. Then you can use map() to get a specific property from each of the filtered items.

const organizeNames = function(foods) {
  let foodNames = foods.filter((names) => names.food == 'oranges').map(names => names.name);
  return foodNames;
}

console.log(
  organizeNames([{
      name: 'Samuel',
      food: 'oranges'
    },
    {
      name: 'Victoria',
      food: 'pizza'
    },
    {
      name: 'Karim',
      food: 'pizza'
    },
    {
      name: 'Donald',
      food: 'pizza'
    },
  ])
);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!