为什么Array.prototype.reduce()不接受Map对象作为初始值? [英] Why wouldn't Array.prototype.reduce() accept a Map object as an initial value?

查看:137
本文介绍了为什么Array.prototype.reduce()不接受Map对象作为初始值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于以下原因,此问题被搁置:



这个问题是由于无法再现的问题或简单的问题印刷错误。



我只想确认问题仍然存在于现在的stackoverflow沙箱,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);

您可以尝试一下这里



OK已经有答案我已经接受,但即使在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。大小); return p},new Map()); document.write(< pre>+ JSON.stringify(mc,null,2)+< / pre>);  


解决方案

你的代码完全正常, code> repl.it 控制台失败。



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

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



更新:序列化地图



为什么Map在 repl.it 和Stackoverflow的代码片段都是将它们转换为字符串。



为了获得Map的有意义的字符串表示,您应该覆盖其 toString 方法,像这样:

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

  var myClients = {'cccc':{newValue:'test three',timeTest:3},'bbbb':{newValue:'test two',timeTest:2},'aaaa':{newValue:'test one' timeTest:1}};类SerializableMap扩展Map {toString(){const res = {}; for(let entry 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) / pre> 



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


OK this question was put on hold for the following reason;

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

I just would like to confirm that the problem still exists as of now on stackoverflow sandbox, repl.it sanbox and even on firefox console and it is certainly not a typo. Copy paste the code to Firefox console and it will not work as it should.

I have been trying to sort several objects in a Map object according to one specific property. I prefer to use the arrow functions and array methods. It seems like Array.prototype.reduce() wouldn't accept a fresh Map object as an initial parameter to start with. What i also don't understand is, while the Map object wouldn't get populated, at the end it's size shows the correct value as if it does. Can anybody tell me what's happening here..? Why the Map object looks empty while it's size shows 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);

You may try it out here.

OK there have been answers which i have accepted but even in the stackjoverflow sandbox it wouldn't show properly. Check it up

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>");

解决方案

Your code is perfectly fine, it's a repl.it console that fails.

You can check that your code is correct by accessing a Map entries by key:

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

Update: serializing Map

The reason why Map looks empty in repl.it and Stackoverflow's snippet is that both are converting it to string.

In order to get meaningful string representation of a Map, you should overwrite its toString method, like so:

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)

Now you can easily see the state of your Map.

这篇关于为什么Array.prototype.reduce()不接受Map对象作为初始值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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