1

I'm getting the error 'Expected identifier' in Internet Explorer. Works in all other browsers.

                for(var [i, article] of articles.entries()) {
                  if(article.hero) {
                        heroes.unshift(article);
                        articles.splice(i, 1);
                        break;
                    }
                }

Any help on this would be much appreciated.

Thanks

Fazberry
  • 117
  • 8
  • 3
    **Go to** [Destructuring assignment compatibility section](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment#Browser_compatibility) – Ele Jul 03 '19 at 09:00
  • 1
    Destructuring is not supported in IE. – VLAZ Jul 03 '19 at 09:02
  • 2
    Go to [Object.entries](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries#Browser_compatibility) also, this is also not supported – Code Maniac Jul 03 '19 at 09:03
  • 2
    ...and [`for...of`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of#Browser_compatibility) – VLAZ Jul 03 '19 at 09:04
  • Related: [Support for ES6 in Internet Explorer 11](https://stackoverflow.com/questions/39902809/support-for-es6-in-internet-explorer-11) – Ivar Jul 03 '19 at 09:07
  • For..of loop is not supported in Internet Explorer. As an alternative, you can try to make a test with for..in loop or for loop may help your code to work in IE. Ref: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in and https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for – Deepak-MSFT Jul 03 '19 at 09:23
  • Possible duplicate of [Support for ES6 in Internet Explorer 11](https://stackoverflow.com/questions/39902809/support-for-es6-in-internet-explorer-11) – VLAZ Jul 03 '19 at 09:59

3 Answers3

0

Go to Destructuring assignment compatibility section

Basically, the destructuring approach is not supported by IE, so you should modify your logic for a wider supported approach.

For example:

for (var i in articles) {
    var article = articles[i];
    if (article.hero) {
        heroes.unshift(article);
        articles.splice(i, 1); // (Pay attention) variable i should be a number.
        break;
    }
}
Ele
  • 33,468
  • 7
  • 37
  • 75
0

Beow code should work:-

for (var _iterator = articles.entries(), _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
  var _ref;

  if (_isArray) {
    if (_i >= _iterator.length) break;
    _ref = _iterator[_i++];
  } else {
    _i = _iterator.next();
    if (_i.done) break;
    _ref = _i.value;
  }

  var _ref2 = _ref,
      i = _ref2[0],
      article = _ref2[1];

  if (article.hero) {
    heroes.unshift(article);
    articles.splice(i, 1);
    break;
  }
}

for conversion of javascript code reference :- https://babeljs.io/repl

shotgun02
  • 756
  • 4
  • 14
0

Object and Array destructuring is not supported in IE. Neither is Object.entries. Instead try using a normal for loop.

for (var i = 0; i < articles.lenght; articles++) {
  var article = articles[i];
  if (article.hero) {
    heroes.unshift(article);
    articles.splice(i, 1);
    break;
  }
}
nick zoum
  • 7,216
  • 7
  • 36
  • 80