I'm trying to work with Rethinkdb in mapreducing something like:
[{"a":1, "b":2}, {"a":3}, {"c":4}] to {"a":[1,3], "b":[2], "c":[4]}.
I have already consulted Javascript: How convert array of objects to object with sorted unique arrays? but the statements doesn't really work in ReQL, here's an example:
r.expr([{"a":1, "b":2}, {"a":3}, {"c":4}]).do(function(user){
var c1 = {};
var keys = ["a", "b", "c"];
user.map(function(itm){
keys.map(function(p){
if(!c1[p]){
c1[p] = [];
};
c1[p] = r.expr(c1[p]).setInsert(itm(p).default("NIL"));
});
return 1
});
return c1
});
but this faults out on itm(p) with the error: RqlCompileError: Variable name not found in:
RqlCompileError: Variable name not found in:
r([{a: 1, b: 2}, {a: 3}, {c: 4}]).do(function(var_136) { return {a: r([]).setInsert(var_137("a").default("NIL")), b: r([]).setInsert(var_137("b").default("NIL")), c: r([]).setInsert(var_137("c").default("NIL"))}; })
^^^^^^^
because rethinkdb is assigning variable id (137 in this case) to the itm(p) that's not declared beforehand.
Any ideas how i can do this?
Thanks
['a','b','c']to specify the order of the keys in the returned object, but javascript Objects do not have an order, the keys may be returned in any sequence regardless of the order they were added (or alphabetic or numeric order). In practice, most browsers will return object properties in the order they were added, but that is not guaranteed and is inconsistent in some browsers in some cases and can't be relied on.