-4

I need to export JSON data to CSV file.I followed this link to export my data to csv file. Exporting is working perfectly in chrome but it is throwing syntax error (for Arrow function) in IE.

ArrowFunction

// format the data
itemsNotFormatted.forEach((item) => {
    itemsFormatted.push({
        model: item.model.replace(/,/g, ''), // remove commas to avoid errors,
        chargers: item.chargers,
        cases: item.cases,
        earphones: item.earphones
    });
});

I'm new to javascript. Can you please guide me to write the same function that will support in IE?

Please refer this link for full code.

Thanks

bluebud
  • 183
  • 1
  • 1
  • 10
  • 2
    Why not read some DOCS? There's a clear example on how to transform a traditional function to Arrow function: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions - `(item) => {` becomes `function(item) {` - Also: https://babeljs.io/ and a nice read: https://survivejs.com/ – Roko C. Buljan Feb 04 '21 at 09:36
  • 1
    You can either switch the arrow function with a normal function or use a builder like babel/webpack to transform new features automatically – mousetail Feb 04 '21 at 09:37

2 Answers2

0

Arrow functions are not supported in IE browsers. Check out https://caniuse.com/arrow-functions

Replace fat arrow syntax with function..

// format the data
itemsNotFormatted.forEach(function(item) {
    itemsFormatted.push({
        model: item.model.replace(/,/g, ''), // remove commas to avoid errors,
        chargers: item.chargers,
        cases: item.cases,
        earphones: item.earphones
    });
});
Varun
  • 241
  • 1
  • 9
  • @bluejayke probably because this question is a simple issue that is marked as a duplicate and thus does not require an answer. – kemicofa ghost Feb 04 '21 at 09:43
-2

You don't have to use the arrow function, you can use a regular one instead

// format the data
itemsNotFormatted.forEach(function(item) {
    itemsFormatted.push({
        model: item.model.replace(/,/g, ''), // remove commas to avoid errors,
        chargers: item.chargers,
        cases: item.cases,
        earphones: item.earphones
    });
});
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • 2
    I didn't downvote, but likely because there are so many duplicates about that topic. – t.niese Feb 04 '21 at 09:38
  • In that case you should down-vote the question, not the answer – mousetail Feb 04 '21 at 09:39
  • 1
    If you answer duplicates over and over again, you hurt the quality of the platform. Answers are community reviewed for quality, if you write just another answer for which a duplicate already exists, instead of flagging it as duplicate, you waste the time of other community members because they need to check if the answer is correct. Furthermore, answers can be improved in the future (e.g. suggesting things how to solve the problem but still using arrow functions) if there are many duplicates then this information might be scattered over multiple questions making it hard to find. – t.niese Feb 04 '21 at 09:45
  • @t.niese so would you prefer it if I said nothing at all? then how would the OP find help? Also how am I supposed to know its a duplicate? – B''H Bi'ezras -- Boruch Hashem Feb 04 '21 at 09:46
  • @bluejayke through the duplicate marked answer and with 2k rep you should have marked this question as a dup. A simple google search "ie arrow functions" will give you [this](https://stackoverflow.com/questions/40216015/why-doesnt-this-arrow-function-work-in-ie-11) result. – kemicofa ghost Feb 04 '21 at 09:48
  • @kemicofaghost it was marked as a duplicate **after** this answer was posted, should I be expected to do an in depth research project to ensure its definitely not asked anywhere else on the internet, of every question on here before attempting to answer? – B''H Bi'ezras -- Boruch Hashem Feb 04 '21 at 09:49
  • 1
    @bluejayke `should I be expected to do an in depth research project of every question on here before attempting to answer` yes you shoudl check if it is answered **here** on stackoverflow. – t.niese Feb 04 '21 at 09:50
  • 2
    @bluejayke yes obviously. Are you here to help make the platform a better place or here just to focus on grinding rep ? – kemicofa ghost Feb 04 '21 at 09:50
  • @kemicofaghost if I see a question that I know the answer to, I'm answering it, theres no possible way I can know if its already been asked or not – B''H Bi'ezras -- Boruch Hashem Feb 04 '21 at 09:51
  • @t.niese how am I supposed to check that? For every possible question on here, I shouldn't even attempt to answer at all, unless I've searched through every possible question, 15 million+? – B''H Bi'ezras -- Boruch Hashem Feb 04 '21 at 09:51
  • 1
    What you are expected to do is to either use the search here on StackOverflow and check if a fast search for e.g. `arrow function IE` gives any results and maybe cross-check that with a google search like `site:stackoverflow.com arrow function IE`. And if you find a question there (which in this case is on the first page in both cases) either vote to close or leave a comment targeting to that question. – t.niese Feb 04 '21 at 09:57
  • 2
    Personally, I don't think that one should downvote answers to duplicates, but I can understand why others do so. – t.niese Feb 04 '21 at 09:58