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

163
Vistas
How to find all indexes of arrays having a certain value with Javascript?

In the following example code, how can I find all the indexes of any array of year 2021 in the dates? Thank you for any guide!

function test() {
  const dates = [
    'October 31, 2020',
    'November 30, 2020',
    'December 31, 2020',
    'January 31, 2021',
    'February 28, 2021',
    'March 31, 2021',
  ];


  // How should the following code be changed to get indexes of any array of year 2021, which should be 3,4,5?
  // The following code returned only the first array index, 3, not 3,4,5.
  var indexes = dates.findIndex(el => el.includes('2021'));
  console.log(indexes);
}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Use reduce:

/*<ignore>*/console.config({maximize:true,timeStamps:false,autoScroll:false});/*</ignore>*/
function test() {
  const dates = [
'October 31, 2020',
'November 30, 2020',
'December 31, 2020',
'January 31, 2021',
'February 28, 2021',
'March 31, 2021',
  ];


  // The following code returned 3,4,5.
  var indexes = dates.reduce((acc,el,i) => el.includes('2021') ? [...acc,i] : acc,[]);
  console.log(indexes);
}
test()
<!-- https://meta.stackoverflow.com/a/375985/ -->    <script src="https://gh-canon.github.io/stack-snippet-console/console.min.js"></script>

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