DeeTee
DeeTee

Reputation: 707

Javascript syntax error on Internet Explorer

I have the following bit of code which adjusts the headers of some parsed data:

    var parsedData = transposed.map(row =>
        row.reduce((acc, col, ind) => {
            acc[headers[ind]] = col;
            return acc;
        }, { }));

The code works fine on Chrome/Edge/Firefox but fails on Internet Explorer saying there's a syntax error at: row =>.

I'm unable to determine where the syntax error is being thrown - I'm relatively new to javascript's map function so I may be missing something. Where in the above code snippet is the syntax error?

Upvotes: 2

Views: 8067

Answers (2)

nzjun90
nzjun90

Reputation: 281

IE is old and does not understand the arrow function syntax.

reference: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/Arrow_functions

you might need to use the old anonymous function to make it compatible with IE or if polyfill exists, use those. or use transpilers (such as Babel) to transpile it to es5.

example of old annonymous function:

transposed.map(function(row){
        row.reduce((acc, col, ind) => {
            acc[headers[ind]] = col;
            return acc;
   }, { });
});

but remember, you will lose scope of the outside this

Upvotes: 3

user3536179
user3536179

Reputation:

Arrow functions are a core part of the ES6 language feature set.

Those are not directly supported in IE, you need to transpile your code first.

Babel is the go-to transpiler for ES6.

Upvotes: 10

Related Questions