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

293
Visualizações
Using Map and Filter at the same time in JavaScript

I'm trying to use the map and filter example at:

https://masteringjs.io/tutorials/fundamentals/map-filter#:~:text=JavaScript%27s%20Array%23map%20%28%29%20and%20Array%23filter%20%28%29%20functions%20are,function%20works%20fine%20on%20an%20array%20of%20numbers.

var data = [
        {
            "pmid": 12637528,           
            "citation_count": 75
        },
        {
            "pmid": 12732634,          
            "citation_count": 49
        },
        {
            "pmid": 15118089,           
            "citation_count": 88
        }
]

I am using

let iIndex =  data.map(d => d.citation_count).filter( d.citation_count >=50);

and

 let iIndex =  data.map(d => d.citation_count).filter( data.citation_count >=50);

but I get the following error: "err = ReferenceError: d is not defined at eval" or with data.citation err = TypeError: false is not a function at Array.filter

If I take the .filter off it works fine, and I get an array of just the citation count. Can someone help with the correct syntax for the filter portion? I have searched stackOverflow but there are a lot of answers that are about 10 years old and I'm not sure what the most current direction has gone. I have even seen a mention of flatMap. Thanks for the help

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

It's unnecessary to have d.citation_count in filter:

let iIndex =  data.map(d => d.citation_count).filter( d => d >=50);
about 4 years ago · Juan Pablo Isaza Relatório

0

The reason your code is failing is because filter expects a function that returns a boolean - you have provided it with an expression.

A simple fix to your code would be to wrap your expression in an anonymous function:

const result = data.map(item => item.citation_count).filter(count => count >= 50)

You could also just use reduce, which combines the effect of both the filter and map functions, making the code more efficient:

const data = [
    { pmid: 12637528, citation_count: 75 },
    { pmid: 12732634, citation_count: 49 },
    { pmid: 15118089, citation_count: 88 },
]

const result = data.reduce((result, { citation_count }) => {
  if (citation_count >= 50) {
    result.push(citation_count)
  }
  return result
}, [])

console.log(result)

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