【问题标题】:Arrow function (=>) is not supporting in IE [duplicate]IE中不支持箭头功能(=>)[重复]
【发布时间】:2021-05-08 14:12:42
【问题描述】:

我需要将 JSON 数据导出到 CSV 文件。我按照this 链接将我的数据导出到 csv 文件。 导出在 chrome 中运行良好,但在 IE 中抛出语法错误(箭头函数)。

箭头函数

// 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
    });
});

我是 JavaScript 新手。能否请您指导我编写将在 IE 中支持的相同功能?

完整代码请参考link。

谢谢

【问题讨论】:

标签: javascript ecmascript-6 export-to-csv es6-modules arrow-functions


【解决方案1】:

你不必使用箭头功能,你可以使用普通的代替

// 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
    });
});

【讨论】:

  • 我没有投反对票,但可能是因为该主题有太多重复项。
  • 如果你一遍又一遍地回答重复,你会损害平台的质量。答案经过社区质量审查,如果您只写另一个已经存在重复的答案,而不是将其标记为重复,您会浪费其他社区成员的时间,因为他们需要检查答案是否正确。此外,将来可能会改进答案(例如,建议如何解决问题但仍然使用箭头函数)如果有很多重复,那么这些信息可能会分散在多个问题中,因此很难找到。
  • @bluejayke should I be expected to do an in depth research project of every question on here before attempting to answer 是的,你应该检查它是否在 here 在 stackoverflow 上得到了回答。
  • @bluejayke 显然是的。您来这里是为了帮助使平台变得更好,还是只是为了专注于打磨代表?
  • 就个人而言,我不认为应该对重复的答案投反对票,但我可以理解为什么其他人会这样做。
【解决方案2】:

IE 浏览器不支持箭头函数。查看https://caniuse.com/arrow-functions

用函数替换粗箭头语法..

// 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
    });
});

【讨论】:

  • @bluejayke 可能是因为这个问题是一个简单的问题,被标记为重复,因此不需要答案。
猜你喜欢
  • 2021-12-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-31
  • 2017-12-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多