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

155
Visualizações
Use filter on an array for a specific index?

Basic question but its something that I've never quite understood with these array methods.

Say I have an array such as:

[[ 'PENNY', 0 ],[ 'NICKEL', 0 ],[ 'DIME', 20 ],[ 'QUARTER', 50 ]] 

and I wanted to use filter to return a new array in which only elements are returned whose first index is greater than 0.

So it would return an array like so:

[ [ 'DIME', 20 ],[ 'QUARTER', 50 ]].

How would I go about this using filter? Some clarification would be appreciated.

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

0

Just use filter as you mention in question for compare if value is > of 0 like:

const array =  [[ 'PENNY', 0 ],[ 'NICKEL', 0 ],[ 'DIME', 20 ],[ 'QUARTER', 50 ]];
console.log(array.filter(el => el[1] > 0));

Reference:

  • Array.prototype.filter()
about 4 years ago · Juan Pablo Isaza Relatório

0

Filter by the second element:

const array = [[ 'PENNY', 0 ],[ 'NICKEL', 0 ],[ 'DIME', 20 ],[ 'QUARTER', 50 ]];

const filtered = array.filter(([, count]) => count > 0);

console.log(filtered);

[, count] is destructuring, which you can read about in full here

about 4 years ago · Juan Pablo Isaza Relatório

0

const array = [[ 'PENNY', 0 ],[ 'NICKEL', 0 ],[ 'DIME', 20 ],[ 'QUARTER', 50 ]]
 
const arrayFiltered = array.filter((item) => item[1] > 0)
console.log(arrayFiltered)

filter() method return a new array based on the rule you specify inside. In this case, we want a new array with item[1] (the index of our array) to be > 0.

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