badigard
badigard

Reputation: 850

Translate ES6 'for' loop from ES5 'for-each' loop

I need help with translating the following es6 loop to es5 code.

for (let [field_name, field] of impList) {
//// some code
}

Thanks.

Upvotes: 1

Views: 6029

Answers (1)

CodingIntrigue
CodingIntrigue

Reputation: 78545

Assuming that impList is an Array (or an array-like object), and not an ES6 Iterable type (which would require polyfills, etc), you can roughly translate that to a for loop:

for (var i=0; i<impList.length; i++) {
    var field_name = impList[i][0];
    var field = impList[i][1];
}

Or a forEach:

impList.forEach(function(entry) {
    var field_name = entry[0];
    var field = entry[1];
});

In addition to impList possibly being an Iterable, there are some nuances here that I didn't translate to ES5, because there are quite a lot of caveats. Which is why you should be using a transpiler such as Babel.

Upvotes: 8

Related Questions