0

I'm trying to iterate a Javascript Set in Stackblitz: https://stackblitz.com/edit/javascript-set-iterate?file=index.js

for (const n of s) {
  array.push(n);
  console.log(n);
}

However the for loop has never been entered somehow.

When I opened the Source tab in Chrome Dev Tools and I saw it's translated as:

for (var _i = 0, s_1 = s; _i < s_1.length; _i++) {
    var n = s_1[_i];
    array.push(n);
    console.log(n);
}

Apparently there is no length property for Set in Javascript. It should be size: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set So the loop is skipped by mistake.

If I run the same for...of code in the browser console it works fine.

Question:

I'm not sure whether it's my Stackblitz configuration wrong (compiler selection of ES5/ES6 etc.?) or it's a Stackblitz bug?

Thanks!

Krist Jin
  • 61
  • 7
  • It should've been `s_1 = Array.from(s)`. Probably a bug – adiga Jun 13 '20 at 10:11
  • 1
    This sounds like indeed there's something wrong with the transpilation settings (e.g. the [`assumeArray` flag being set](https://babeljs.io/docs/en/babel-plugin-transform-for-of#assumearray)), but I don't know where to change that in Stackblitz. – Bergi Jun 13 '20 at 10:29

0 Answers0