为什么 Array.prototype.reduce() 不接受 Map 对象作为初始值?

Why wouldn't Array.prototype.reduce() accept a Map object as an initial value?

好的,这个问题由于以下原因被搁置;

"This question was caused by a problem that can no longer be reproduced or a simple typographical error. "

我只是想确认问题目前在 Whosebug 沙箱、repl.it sanbox 甚至 firefox 控制台上仍然存在,这肯定不是错字。将代码复制粘贴到 Firefox 控制台,它不会正常工作。

我一直在尝试根据一个特定的 属性 对 Map 对象中的多个对象进行排序。我更喜欢使用箭头函数和数组方法。似乎 Array.prototype.reduce() 不接受新的 Map 对象作为初始参数。我也不明白的是,虽然 Map 对象不会被填充,但最后它的大小显示了正确的值,就好像它确实存在一样。谁能告诉我这里发生了什么……?为什么 Map 对象看起来是空的,而它的大小显示为 3。

var myClients = {
    'cccc': { newValue: 'test three', timeTest: 3 },
    'bbbb': { newValue: 'test two', timeTest: 2 },
    'aaaa': { newValue: 'test one', timeTest: 1 }
};

var mc = Object.keys(myClients)
    .sort((p,c) => myClients[p].timeTest <= myClients[c].timeTest ? -1 : 1)
    .reduce((p,c) => {p.set(c, myClients[c]); console.log(p.size); return p}, new Map());

console.log(mc, mc.size);

你可以试试here

好的,我已经接受了一些答案,但即使在 stackjoverflow 沙箱中,它也无法正确显示。检查一下

var myClients = {
    'cccc': {
      newValue: 'test three',
      timeTest: 3
    },
    'bbbb': {
      newValue: 'test two',
      timeTest: 2
    },
    'aaaa': {
      newValue: 'test one',
      timeTest: 1
    }
  },
  mc = Object.keys(myClients)
    .sort((p, c) => myClients[p].timeTest <= myClients[c].timeTest ? -1 : 1)
    .reduce((p, c) => {
      p.set(c, myClients[c]);
      console.log(p.size);
      return p
    }, new Map());
document.write("<pre>" + JSON.stringify(mc, null, 2) + "</pre>");

不要相信 console.log 进行调试;实际使用 the debugger 会更好,这样您就可以自己检查对象了。

在这种情况下,您的代码似乎工作正常; 运行 片段并在浏览器的控制台中查看,您应该会看到预期的结果。 repl.it 对 console.log 的实现似乎只是将 Map 的所有实例记录为 "{}"

var myClients = {
    'cccc': {
      newValue: 'test three',
      timeTest: 3
    },
    'bbbb': {
      newValue: 'test two',
      timeTest: 2
    },
    'aaaa': {
      newValue: 'test one',
      timeTest: 1
    }
  },
  mc = Object.keys(myClients).sort((p, c) => myClients[p].timeTest <= myClients[c].timeTest ? -1 : 1).reduce((p, c) => {
    p.set(c, myClients[c]);
    console.log(p.size);
    return p
  }, new Map());
console.log(mc, mc.size);

您的代码非常好,它是一个失败的 repl.it 控制台。

您可以通过按键访问地图条目来检查您的代码是否正确:

mc.has('cccc') // true
mc.get('cccc') // { newValue: 'test three', timeTest: 3 }

更新:序列化地图

Map 在 repl.it 和 Whosebug 的代码片段中看起来是空的原因是两者都将其转换为字符串。

为了获得有意义的 Map 字符串表示,您应该重写其 toString 方法,如下所示:

class SerializableMap extends Map {
  toString() {
    const res = {};
    for (let entry of this.entries()) {
      res[entry[0]] = entry[1];
    }
    return JSON.stringify(res);
  }
}

var myClients = {
    'cccc': { newValue: 'test three', timeTest: 3 },
    'bbbb': { newValue: 'test two', timeTest: 2 },
    'aaaa': { newValue: 'test one', timeTest: 1 }
};

class SerializableMap extends Map {
  toString() {
    const res = {};
    for (let entry of this.entries()) {
      res[entry[0]] = entry[1];
    }
    return JSON.stringify(res);
  }
}

var mc = Object.keys(myClients)
    .sort((p,c) => myClients[p].timeTest <= myClients[c].timeTest ? -1 : 1)
    .reduce((p,c) => {p.set(c, myClients[c]); console.log(p.size); return p}, new SerializableMap());

document.write(mc)

现在您可以轻松查看地图的状态。