javascript - Group subarrays when one value is the same -


i'm trying group data in array of arrays when value @ array[i][0] common.

can use array.map() this?

i've been toying solution, create new array unique values , compare original array, can't figure out logic craft result highcharts wants.

i'm pretty confident if can figure out how array manipulation, can figure out logic automate highcharts, make saturday :)

i have:

var array = [     ["name1","date1"],     ["name1","date2"],     ["name2","date1"],     ["name2","date1"] ]; 

and want turn into:

var array = [     [         ["name1","date1"],         ["name1","date2"]     ],         ["name2","date1"],         ["name2","date2"]     ] ]; 

it may not possible in single loop. inclined create object in first pass, , build data structure in second. like:

var obj = oldarray.reduce(function(memo, item) {   // unique value you're interested in, e.g. "name1"   var key = item[0];   // if you've got it, push value on end   if (memo[key]) {     memo[key].push(item);   } else {     // otherwise, create new array single value     memo[key] = [ item ];   }   // don't forget return memo, or you'll have 1 of   // "bang head against wall" moments   return memo; }, {});  // create new array , push values on var newarray = []; (var k in obj) {   newarray.push(obj[k]); }  // or alternative way same thing var newarray = object.keys(obj).reduce(function(memo, k) {   memo.push(obj[k]);   return memo; }, []); 

Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -