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

144
Visualizações
.filter javascript and defining variable

ok so here is the question i am trying to pull the name of an animal if the population is less than 5 using the .filter method. set lowPop as the new array but it always returning as undefined. im not sure why that is could anyone assist?

I have attached a picture to show what the question is asking along with my code. i am getting that my var is undefined and my code is returning a boolian true or false response. the expected response is in the picture. any assistance is greatly appreciated.

code and question with expected/error pic

function lowPopulationAnimals(){
const lowPop = [];
zooAnimals.filter((element => {
lowPop.push(element.population <= 5 );
}));

console.log(lowPopulationAnimals);
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Note that:

The filter() method creates a new array with all elements that pass the test implemented by the provided function.

It is for create a new array, not for modifying the new array.

Also, you should not use push in array.filter, filter is running a test to the array and create the copy to all the element in the array that pass the test.

Also, you are directly assigning function_name to console which is incorrect.

Use:

function lowPopulationAnimals(){
let lowPop = [];
 lowPop = zooAnimals.filter(element =>element.population <= 5 )
console.log(lowPop);
}

lowPopulationAnimals();

about 4 years ago · Juan Pablo Isaza Relatório

0

When you pass a function to another function (as you are doing to "filter") it passes a function that only has access to the data you provide it. When filter is called, it passes each element to the function you provide it and if the value it returns evaluates to true then it keeps it, otherwise it removes it. Look into callback functions, and see here in the documentation.

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