c# - Pivot json data from array of models -


i host web api service, returns snapshot of model "mymodel" last 12 months. json array receive on client simple array of 12 "mymydel" objects.

if mymodel has properties "foo" , "bar", there efficient way in javascript pivot array result in collection of arrays of properties, example:

i'd transform

history{   mymodel[]      mymodel[0]        foo: 1        bar: 2        key: 1      mymodel[1]        foo: 3        bar: 4        key: 1  } 

into

history{     "1"{  //key       foo[]          foo[0]: 1         foo[1]: 3       bar[]          bar[0]: 2          bar[1]: 4       }   } 

i open implementing on server side if more efficient. api service .net 4.5 web api.

so again. question is, there super efficient way achieve these results in javascript? if not, there 1 in c#? can provide simple example code me going?

help appreciated. thanks!

edit: working code

even though voted down on this, figure i'd provide working example of javascript achieves this. it's pretty fast too. note each history record must have key group records by.

  _pivot = function(history) {     var i, pivoted, prop, rec;     pivoted = {};     if (history !== null) {       = 0;       while (i < history.length) {         rec = history[i];         pivoted[rec.key] = pivoted[rec.key] || {};         (prop in rec) {           pivoted[rec.key][prop] = pivoted[rec.key][prop] || [];           if (!pivoted[rec.key][prop].date) {             pivoted[rec.key][prop].push({               value: rec[prop]             });           }          }         i++;       }       return pivoted;     }     return null;   }; 

well, far super efficient, think unless change underling model, going have iterate through model, either server side or client side , assign each property different array.

var tempfoo = newarray(); tempbar = newarray(); (var = 0; < mymodel.length; i++){     tempfoo.push(mymodel[i].foo);     tempbar.push(mymodel[i].bar); } 

also, if have sorted in server side code, or if automatically sorted whatever database pulling from, see speed increases. why faster process sorted array unsorted array?

hope helps, fyi, example javascript.

v/r


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -