Redu Redu - 7 months ago 9
Javascript Question

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

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




Answer

Don't trust console.log for debugging; it's much better to actually use the debugger so you can inspect the objects yourself.

In this case, your code seems to work fine; run the snippet and look in your browser's console and you should see what you expect. repl.it's implementation of console.log just seems to log all instance of Map as "{}".

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

Comments