1

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

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

Thanks.

2
  • 2
    That depends a lot on what impList is. A Map? An array? Commented Jun 2, 2016 at 14:37
  • Have you tried using a transpiler? Use Babel. Commented Jun 2, 2016 at 14:38

1 Answer 1

8

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.

Sign up to request clarification or add additional context in comments.

4 Comments

to be honest, I dont understand Babel's code: for (var _iterator = impList.entries()[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { var _step$value = _slicedToArray(_step.value, 2); var field_name = _step$value[0]; var _field = _step$value[1]; }
Babel's output is written for computers, not humans :) It includes polyfills, shims & functionality to address the nuances I described above. All you need to know is you put in ES6 and it outputs valid ES5!
When I put the es5 code, I get error on 'Symbol' object.
My project is in ES5. I am refactoring ES6 to ES5. Do I need to include it to write ES5?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.