I have a question regarding underscore's reduce behaviour with an object.
Given the following object:
var data = [ { type: "A", val: 2 },
{ type: "B", val: 3 },
{ type: "R", val: 3 },
{ type: "L", val: 3 },
{ type: "R", val: 3 },
{ type: "A", val: 1 },
{ type: "B", val: 5 } ];
And the following function:
output = _(data).reduce(function(mem, d) {
console.log(mem);
mem[d.type] = d.type;
return mem
}, {});
The output in the console for all 8 iterations is:
Object { A: "A", B: "B", R: "R", L: "L" }
1) Why is this happening, shouldn't the first output not be empty as I've given it a {} as my initial state?
2) Why is it the case that all the unique types appear to have been found from the first iteration?
Any help would be appreciated, thanks.