Using forEach or for loop to split array of arrays into two arrays

Working on the d3 challenge but this is more a vanilla JS issue. Pen here.

I have pulled JSON data which has more than a I need and defined the dataset to just be the “data” part of the JSON. That much is working. However, when I try to split the data array into two arrays, it doesn’t work.

Working part:

d3.json("").then(function(data){    //console.log(data["data"]);
  const dataset = data["data"]; 
  console.log(dataset[0], dataset.length, "got data")
  const w = 500;
  const h = 100;

Right now I am focused on getting the second entry of each subarray. (This is GDP data, hence my name for the variable.)

Not so working part (0 for 3 approaches:

let gdps = dataset.mep(function(item) {
   return item[1];
 let gdps2 = [];
 for(let i = 0; i < dataset.length, i++) {
   gdps2[i] = dataset[i][1]
 let gdps3 = dataset.forEach(function(item) {

I can’t see what I’m doing wrong, and getting no errors in the console or browser.


This is a typo.

To my knowledge, you can’t set the value of an array like this. Try using push.

Append is for DOM manipulation. Doesn’t work on arrays.

The push method didn’t work on 2 and 3 but fixing the typo took care of 1. Thanks!

1 Like

Hmm, it should have worked on method 2. Method 3 would have needed some more tweaks, though. :slight_smile:

But glad you got one to work! :smiley: