Javascript convert array -
i have array of google analytics data:
[ ["2016-01-01","google","335"], ["2016-01-01","bing","135"], ["2016-01-01","yahoo","35"], ["2016-01-02","google","145"], ["2016-01-02","bing","115"], ........... ] it can large. simplest , fastest way following 2 arrays?
an array of unique date values:
["2016-01-01","2016-01-02","2016-01-03","2016-01-04"] - uniqe sorting date an array of objects group data source:
[{      source: 'google',      data: [335, 145,.....] // lenght array=count unique date }, {     source: 'bing',     data: [135, 115,.....] }, ... ] 
a solution intermediate object.
var array = [["2016-01-01", "google", "335"], ["2016-01-01", "bing", "135"], ["2016-01-01", "yahoo", "35"], ["2016-01-02", "google", "145"], ["2016-01-02", "bing", "115"]],      object = {},      keys = {},      uniquedata,      grouped = [];    array.foreach(function (a) {      keys[a[0]] = true;      object[a[1]] = object[a[1]] || {};      object[a[1]][a[0]] = a[2];  });  uniquedata = object.keys(keys).sort();  grouped = object.keys(object).map(function (k) {      return {          source: k,          data: uniquedata.map(function (a) {              return object[k][a] || 0;          })      };  });    document.write('<pre>' + json.stringify(uniquedata, 0, 4) + '</pre>');  document.write('<pre>' + json.stringify(grouped, 0, 4) + '</pre>');
Comments
Post a Comment