Asked
Updated
Viewed
8.2k times

I am currently working with the Google Analytics API and the response is in JSON. Part of the response is below. I need to split this into two arrays using JavaScript. The first value should be an array called dates, and the second needs to be an array called traffic.

How would you go about doing this? Whats the best method?

"rows":[
      [
         "20210923",
         "137"
      ],
      [
         "20210924",
         "136"
      ],
      [
         "20210925",
         "61"
      ],
      [
         "20210926",
         "72"
      ],
      [
         "20210927",
         "145"
      ],
      [
         "20210928",
         "158"
      ],
      [
         "20210929",
         "131"
      ],
      [
         "20210930",
         "136"
      ],
      [
         "20211001",
         "117"
      ],
      [
         "20211002",
         "54"
      ],
      [
         "20211003",
         "77"
      ],
      [
         "20211004",
         "125"
      ],
      [
         "20211005",
         "119"
      ],
      [
         "20211006",
         "155"
      ],
      [
         "20211007",
         "127"
      ],
      [
         "20211008",
         "104"
      ],
      [
         "20211009",
         "62"
      ],
      [
         "20211010",
         "53"
      ],
      [
         "20211011",
         "132"
      ],
      [
         "20211012",
         "162"
      ],
      [
         "20211013",
         "132"
      ],
      [
         "20211014",
         "140"
      ],
      [
         "20211015",
         "83"
      ],
      [
         "20211016",
         "52"
      ],
      [
         "20211017",
         "53"
      ],
      [
         "20211018",
         "165"
      ],
      [
         "20211019",
         "146"
      ],
      [
         "20211020",
         "159"
      ],
      [
         "20211021",
         "152"
      ],
      [
         "20211022",
         "109"
      ],
      [
         "20211023",
         "45"
      ]
   ]
  • 0
    Is this Joe? Saw you a few years back at MozCon, you were a great speaker! As far as your question, I believe I answered it the way you wanted, but it might also make sense to extract these into an associative array so that the data always remains tied together. For example with an associative array you would have sets of data like: {'20210928': 158, '20210929': 131, ...} , so that all of your keys are the dates and when you access any date you will get the resulting value. This only works if each date is unique, however. — Brian Wozeniak
  • 1
    Hey Brian, yes this is Joe! LOL Great to meet you, and I am glad you liked my talk at MozCon. Thank's so much for your response. I haven't tried it yet. Btw, its good to see you here again. I have had an account here for 14 years but haven't been active in a very long time. But I do remember you from other threads a long time ago. Great to see you are still active. — Joe Hall
add a comment
2

3 Answers

  • Votes
  • Oldest
  • Latest
Answered
Updated

Let's assume that your JSON response is stored in a JavaScript variable called jsonData. If that is the case I believe the following would achieve your goal:

let dates = [];
let traffic = [];

// Goes through each array set and separates them into the dates and traffic arrays
jsonData.forEach(function(values) {
    dates.push(values[0]);
    traffic.push(values[1]);
});

  • 1
    This works great! Thanks so much! — Joe Hall
  • 1
    forEach() would be more appropriate than map() in this context, since you're not returning a value. — spork
add a comment
1
Answered
Updated

It would be better to consider using for-loop instead of .forEach from a performance perspective. Especially if you expect to be working with a very large input data.

function parse(rows) {
  const dates = [];
  const traffic = [];

  for (const [dateValue, trafficValue] of rows) {
    dates.push(dateValue);
    traffic.push(trafficValue);
  }

  return { dates, traffic };
}
add a comment
1
JO
184 4
Answered
Updated

It's been awhile, and a lot has changed since I last played with JS. Here's another way to accomplish the task, though I'm not sure how efficient it is compared to alternatives.

let analyticsData = '{"rows":[["20210923", "137"], ["20210924", "136"], ["20210925", "61"]]}';

// Since there's only 2 elements we can just flatten this now.
let dates	= JSON.parse(analyticsData).rows.flat();
let traffic	= [];

for(i = dates.length-1; i > 0; i -= 2)
{// shrink the dates array as we add to the traffic array
	traffic.unshift(dates.splice(i, 1)[0]);
}

console.log(dates);
console.log(traffic);
add a comment
1