How to read a javascript array and reformat it as JSON objects?

  • A+
Category:Languages

I'm getting following data from Google Analytics API according to my given parameters. This is a JavaScript array object.

[ [ '201801', '(Other)', '129' ],   [ '201801', 'Direct', '2236' ],   [ '201801', 'Email', '2' ],   [ '201801', 'Organic Search', '6263' ],   [ '201801', 'Referral', '185' ],   [ '201801', 'Social', '669' ],   [ '201802', '(Other)', '371' ],   [ '201802', 'Direct', '2037' ],   [ '201802', 'Email', '3' ],   [ '201802', 'Organic Search', '5790' ],   [ '201802', 'Referral', '162' ],   [ '201802', 'Social', '515' ],   [ '201803', '(Other)', '213' ],   [ '201803', 'Direct', '2465' ],   [ '201803', 'Organic Search', '8596' ],   [ '201803', 'Referral', '238' ],   [ '201803', 'Social', '356' ],   [ '201804', '(Other)', '65' ],   [ '201804', 'Direct', '1872' ],   [ '201804', 'Email', '1' ],   [ '201804', 'Organic Search', '9275' ],   [ '201804', 'Referral', '170' ],   [ '201804', 'Social', '307' ],   [ '201805', '(Other)', '35' ],   [ '201805', 'Direct', '2429' ],   [ '201805', 'Email', '2' ],   [ '201805', 'Organic Search', '8995' ],   [ '201805', 'Referral', '234' ],   [ '201805', 'Social', '341' ],   [ '201806', 'Direct', '51' ],   [ '201806', 'Organic Search', '282' ],   [ '201806', 'Referral', '1' ], [ '201806', 'Social', '3' ] ] 

If is not clear at all, please check this GIST : https://gist.github.com/chanakaDe/3ad4e2a51c99386a2737b65a82f034b1

In this array, you can see '201801' like this. It means YEAR is 2018 and MONTH is 1st. It continues likewise.

Normally for one specific month, we are getting 6 values like (Other) , Direct , Email , Organic Search . Referral and Social. In this array, same date is repeating 6 times.

What I want to do is create a simple JSON object out of this. This is the format I want to create in order to display all these data in my AngularJS front end.

[     {         "date" : "201801",         "(Other)" : "129",         "Direct" : "2236",         "Email" : "2",         "OrganicSearch" : "6263",         "Referral" : "185",         "Social" : "669"     },     {         "date" : "201802",         "(Other)" : "371",         "Direct" : "2037",         "Email" : "3",         "OrganicSearch" : "5790",         "Referral" : "162",         "Social" : "515"     } ] 

I want a JSON array like this. I tried to find duplicate values and then tried to merge them into one JSON object if near by elements same. Many attempts like this. But still no good. Please guys, really wanna your help at this moment. I'm not good for data analytics and these kind of sorting things. HELP me please. This project is done using Node.js 8.10.0.

 


You could take the date as key for a hash table and collect all key value pairs. Later get only the value for an array of objects.

var array = [['201801', '(Other)', '129'], ['201801', 'Direct', '2236'], ['201801', 'Email', '2'], ['201801', 'Organic Search', '6263'], ['201801', 'Referral', '185'], ['201801', 'Social', '669'], ['201802', '(Other)', '371'], ['201802', 'Direct', '2037'], ['201802', 'Email', '3'], ['201802', 'Organic Search', '5790'], ['201802', 'Referral', '162'], ['201802', 'Social', '515'], ['201803', '(Other)', '213'], ['201803', 'Direct', '2465'], ['201803', 'Organic Search', '8596'], ['201803', 'Referral', '238'], ['201803', 'Social', '356'], ['201804', '(Other)', '65'], ['201804', 'Direct', '1872'], ['201804', 'Email', '1'], ['201804', 'Organic Search', '9275'], ['201804', 'Referral', '170'], ['201804', 'Social', '307'], ['201805', '(Other)', '35'], ['201805', 'Direct', '2429'], ['201805', 'Email', '2'], ['201805', 'Organic Search', '8995'], ['201805', 'Referral', '234'], ['201805', 'Social', '341'], ['201806', 'Direct', '51'], ['201806', 'Organic Search', '282'], ['201806', 'Referral', '1'], ['201806', 'Social', '3']],     result = Object.values(         array.reduce((r, [date, key, value]) => {             r[date] = r[date] || { date };             r[date][key] = value;             return r;         }, {})     );  console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: