Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

162
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

0

You could use the indexOf method:

filter.map(el => arr.indexOf(el));
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda