Search code examples
javascriptgoogle-apps-scriptgoogle-analytics-apigoogle-analytics-4

How to combine values from different arrays of object into an array


I want to capture the parameter Unassigned with value 64314 from the API output. My objective is to capture this information in an array such as ['Unassigned',64314]

I used this for-in loop to achieve the above.

var data = obj.rows;

for(var item in data){

 console.log(data[item].dimensionValues);
 console.log(data[item].metricValues);

}


//Output
[{
  value: "Direct"
}]
[{
  value: 1405211
}]....

But I am unable to map it to achieve this ['Unassigned',64314]. To me it seems I have to further loop the second level

//API Output

var obj = {
   "metricHeaders":[
      {
         "name":"sessions",
         "type":"TYPE_INTEGER"
      }
   ],
   "dimensionHeaders":[
      {
         "name":"defaultChannelGroup"
      }
   ],
   "kind":"analyticsData#runReport",
   "rowCount":16.0,
   "rows":[
      {
         "metricValues":[
            {
               "value":789456
            }
         ],
         "dimensionValues":[
            {
               "value":"Direct"
            }
         ]
      },
      {
         "dimensionValues":[
            {
               "value":"Organic Search"
            }
         ],
         "metricValues":[
            {
               "value":7894521
            }
         ]
      },
      {
         "dimensionValues":[
            {
               "value":"Paid Search"
            }
         ],
         "metricValues":[
            {
               "value":789456
            }
         ]
      },
      {
         "dimensionValues":[
            {
               "value":"Cross-network"
            }
         ],
         "metricValues":[
            {
               "value":1254689
            }
         ]
      },
      {
         "dimensionValues":[
            {
               "value":"Email"
            }
         ],
         "metricValues":[
            {
               "value":97766
            }
         ]
      },
      {
         "metricValues":[
            {
               "value":53208
            }
         ],
         "dimensionValues":[
            {
               "value":"Paid Social"
            }
         ]
      },
      {
         "metricValues":[
            {
               "value":64314
            }
         ],
         "dimensionValues":[
            {
               "value":"Unassigned"
            }
         ]
      },
      {
         "metricValues":[
            {
               "value":30069
            }
         ],
         "dimensionValues":[
            {
               "value":"Referral"
            }
         ]
      },
      {
         "metricValues":[
            {
               "value":28790
            }
         ],
         "dimensionValues":[
            {
               "value":"Organic Social"
            }
         ]
      },
      {
         "dimensionValues":[
            {
               "value":"Paid Video"
            }
         ],
         "metricValues":[
            {
               "value":14840
            }
         ]
      },
      {
         "metricValues":[
            {
               "value":11619
            }
         ],
         "dimensionValues":[
            {
               "value":"Display"
            }
         ]
      },
      {
         "metricValues":[
            {
               "value":2165
            }
         ],
         "dimensionValues":[
            {
               "value":"Organic Video"
            }
         ]
      },
      {
         "metricValues":[
            {
               "value":806
            }
         ],
         "dimensionValues":[
            {
               "value":"Paid Other"
            }
         ]
      },
      {
         "dimensionValues":[
            {
               "value":"ABC"
            }
         ],
         "metricValues":[
            {
               "value":261
            }
         ]
      },
      {
         "dimensionValues":[
            {
               "value":"SMS"
            }
         ],
         "metricValues":[
            {
               "value":2
            }
         ]
      },
      {
         "dimensionValues":[
            {
               "value":"Organic Shopping"
            }
         ],
         "metricValues":[
            {
               "value":1
            }
         ]
      }
   ],
   "metadata":{
      "currencyCode":"USD",
      "timeZone":"XXXX"
   }
}



Solution

  • If dimentionaValues & metricValues going to have only one item, then you can specify 1st item and create array dynamically?

    console.log([data[item].dimensionValues[0].value,data[item].metricValues[0].value]);
    

    If they are of same length, you need another loop

    for(var item in data){
      for(var i=0; i< data[item].dimensionValues.length;i++){
     console.log([data[item].dimensionValues[i].value,data[item].metricValues[i].value]);
      }
    }
    

    If they are of different length, you need to specify what logic in question.