Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

269
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!