支持 es5 中的解构分配问题

support issue with destructuring assignments in es5

我遇到了部分代码的问题,该代码使用了 es5 不支持的解构赋值。 (gulp 的构建问题)。

我已经删除了所有 es6 箭头并改用函数,但我不知道如何将解构赋值恢复到 ES5:

var result2 = Object.values(
  zones.reduce(function (a, {id,name,card,request,res}) {
    a[id] || (a[id] = {id, card, name, unique_cards: new Set(), nb_carte: 0, request: {}, res: {} });
    a[id].unique_cards.add(card);
    a[id].nb_carte = a[id].unique_cards.size;
    Object.keys(request).forEach(function (k) {
      (a[id].request[k] = (a[id].request[k] || 0) + request[k])
    });
    Object.keys(res).forEach(function (k) {
      (a[id].res[k] = (a[id].res[k] || 0) + res[k])
    });
    return a;
  }, Object.create(null))
);

实际错误是:

Destructuring assignments are not supported by current JavaScript version

去掉解构,把原来的对象赋值给一个变量(例子中的o)。然后手动将需要的属性分配给变量。

作为 , you should also change the shorthand property names 也可以手动分配。

var result2 = Object.values(
  zones.reduce(function(a, o) {
    var id = o.id, name = o.name, card = o.card, request = o.request, res = o.res;
    a[id] || (a[id] = {
      id: id, // change from shorthand property name
      card: card, // change from shorthand property name
      name: name, // change from shorthand property name
      unique_cards: new Set(),
      nb_carte: 0,
      request: {},
      res: {}
    });
    a[id].unique_cards.add(card);
    a[id].nb_carte = a[id].unique_cards.size;
    Object.keys(request).forEach(function(k) {
      (a[id].request[k] = (a[id].request[k] || 0) + request[k])
    });
    Object.keys(res).forEach(function(k) {
      (a[id].res[k] = (a[id].res[k] || 0) + res[k])
    });
    return a;
  }, Object.create(null))
);