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

267
Vistas
Simple way to enumerate a list in javascript with index?

In python there is a nice shortcut to get the index and element of an interable:

>>> arr=['a','b','c']
>>> for idx, arr in enumerate(arr):
...    print (idx, arr)
...
(0, 'a')
(1, 'b')
(2, 'c')

Would the following be the same in javascript, or would it need to be a generator function instead?

function enumerate1(arr) {
    let out = [];
    for (let idx=0; idx<arr.length; idx++) {
        let elem = arr[idx];
        out.push([idx, elem]);
    }
    return out;
}
let arr1 = ['a','b','c'];
for (let [idx, elem] of enumerate1(arr1)) {
    console.log(idx, elem);
}

// or, a bit more concise
let arr2 = ['a','b','c'];
const enumerate2 = (arr) => arr.map((idx, elem) => [idx, elem]);
for (let [idx, elem] of enumerate2(arr2)) {
    console.log(idx, elem);
}

about 4 years ago · Juan Pablo Isaza
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