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:

  1. why differences of array processing @ map , reduce?

  2. what data structure should use (or how should use array) optimize current solution?

  1. i decided use object:

    var arr = {}; arr[index] = { val: value, count: 1 };

it works for .. in expected.


Comments

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -