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

170
Visualizações
How can I get the indexes of a filtered array items

I have this situation, I have an array and I need to filter it and get the indexes of the filtered items, like this example:

var arr = ['2022-05', '2023-01', '2022-04', '2022-02', '2023-08'];

I'm using this filter:

var filter = arr.filter(e => e.split("-")[0] == '2022'); //To get the values from 2022

And I get this result:

filter = ['2022-05', '2022-04', '2022-02'];

What I need to do now is to get also the index of these items, so it would be this:

filter = ['2022-05', '2022-04', '2022-02'];
index = [0,2,3]

How can I do that? Thanks.

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

0

Before filtering the array you can map it to a new array of objects that include the indexes.

var arr = ['2022-05', '2023-01', '2022-04', '2022-02', '2023-08'];
var output = arr.map((value, index) => ({index, value}))
  .filter(e => e.value.split("-")[0] == '2022');

console.log(output);

about 4 years ago · Juan Pablo Isaza Relatório

0

when matched, just add the desired index to the array

 var arr = ['2022-05', '2023-01', '2022-04', '2022-02', '2023-08'];
    
var index = [];
var filter = arr.filter((e, indx) => {
  const flag = e.split("-")[0] == '2022';
  if (flag) {
    index.push(indx)
  }
  return flag;
});
console.log(filter)
console.log(index)

about 4 years ago · Juan Pablo Isaza Relatório

0

You could use the indexOf method:

filter.map(el => arr.indexOf(el));
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