Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

359
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda