• Empleos
  • Sobre nosotros
  • profesionales
    • Inicio
    • Empleos
    • Cursos y retos
  • empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Evaluaciones
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

127
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 3 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 3 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 3 years ago · Juan Pablo Isaza Denunciar

0

You could use the indexOf method:

filter.map(el => arr.indexOf(el));
about 3 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 Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda