转换reduce函数以使用IE [英] Convert reduce function to work with IE

查看:135
本文介绍了转换reduce函数以使用IE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,所以几个月前我得到了一些帮助,提出了一个解决方案来保持数组中元素的计数:

Alright, so I had some help a couple months ago with coming up with a solution to keep count of the elements in an array: Loop through multiple array and keep count of each element

此解决方案有效完全适合我,直到我意识到它使用 ES6 IE 11 不支持。我试图将它转换为使用函数而不是箭头函数,以便它可以在所有浏览器中工作,但是我遇到了一些问题。

This solution worked perfectly for me until I realized that it's using ES6 which is not supported by IE 11. I've tried to convert it to using functions instead of the arrow function so that it'll work across all browsers but am having some issues.

这是当前的代码在IE中不起作用:

Here is the current code that does not work in IE:

var b = data.reduce((acc, cur) => {
    cur.ProductHandlingTypes.map(({ Name }) => Name).forEach(n => acc[n] = (acc[n] || 0) + 1);
    return acc;
},
{});

如果有人可以指导我在这里需要更改的内容,以便它可以在IE中工作很棒!

If someone could guide me on what needs to be changed here so that it works in IE that would be great!

推荐答案

IE 11不支持箭头功能[1],也不支持解构[2],所以将它转换为ES5语法:

IE 11 doesn't support arrow functions [1], nor destructuring [2], so convert it to ES5 syntax:

var b = data.reduce(function(acc, cur) {
  cur.ProductHandlingTypes
    .map(function(obj) {
      return obj.Name
    })
    .forEach(function(n) {
      return acc[n] = (acc[n] || 0) + 1
    })

  return acc
}, {});

[1] http://caniuse.com/#feat=arrow-functions

[2] http://kangax.github.io/compat-table/es6/#test-destructuring

这篇关于转换reduce函数以使用IE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆