Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

355
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda