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

354
Views
Write a function that takes an array of objects and removes all red-colored foods from the original array (JavaScript)

array, foods = [{ name: 'Apple', color: 'red' }, { name: 'Egg', color: 'white' }]; and here's what I tried,

let foods = [{ name: 'Apple', color: 'red' }, { name: 'Egg', color: 'white' }];

            function removeRed(food) {
                food.filter(function (x) {
                    if (x.color !== 'red') {
                        return true;
                    } else {
                        return false;
                    }
                });
                return foods;

When I call the function like " removeRed(foods) " the output is giving both of the property-value pairs in my array. I'm a beginner student and this is my first Question here. Hope that someone answers :'D }

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

0

you need to return the output of the filter. you were returning the original array

function removeRed(food) {
    return food.filter(function (x) {
        if (x.color !== 'red') {
            return true;
        } else {
            return false;
        }
});
about 4 years ago · Juan Pablo Isaza Report

0

Put return inside the function before the filter called

function removeRed(food) {
  return food.filter(function (x) {
    if (x.color !== 'red') {
      return true;
    } else {
      return false;
    }
  });
}

Call the function to execute the code

let foods = [{ name: 'Apple', color: 'red' }, { name: 'Egg', color: 'white' }];
removeRed(foods); // [{name: 'Egg', color: 'white'}]

Best practice Make always the function more generic like this:

function remove(food, color) {
  return food.filter(function (x) {
    if (x.color !== color) {
      return true;
    } else {
      return false;
    }
  });
}
let foods = [{ name: 'Apple', color: 'red' }, { name: 'Egg', color: 'white' }];
remove(foods,'white'); // [{name: 'Apple', color: 'red'}]

One line with ES6+ syntax:

const remove = (food, color) => food.filter(x=> x.color !== color);
about 4 years ago · Juan Pablo Isaza Report

0

Use this code to filter non-red items

let foods = [{ name: 'Apple', color: 'red' }, { name: 'Egg', color: 'white' }];

const filtered = foods.filter(item => item.color !== 'red');

console.log(filtered)
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!