javascript - What is better way to send associative array through map/reduce at MongoDB? -
here functions:
map:
function () { // initialize key // initialize index (0..65536) // initialize value var arr = []; arr[index] = { val: value, count: 1 }; emit(key, { arr: arr }); }
reduce:
function (key, values) { var result = { arr: [] }; (var = 0; < values.length; i++) { values[i].arr.foreach(function (item, i) { if (result.arr.hasownproperty(i)) { result.arr[i].val += item.val; result.arr[i].count += item.count ; } else { result.arr[i] = item; } }); }
as can see, i'm trying send associative array map reduce. when try enumerate values of array values[i].arr.foreach
listing 0..max_index. so, every reduce have enumerate lot of undefined elements.
when try enumerate values of array (arr
) @ map expected result (only defined elements).
actually, don't sure associative array best solution task. can't find faster way find element id.
could please answer questions:
why differences of array processing @ map , reduce?
what data structure should use (or how should use array) optimize current solution?
i decided use object:
var arr = {}; arr[index] = { val: value, count: 1 };
it works for .. in
expected.
Comments
Post a Comment