0

I am trying to extract imdb data from a csv and put two elements of it into an array of objects. There are 10 fields. I only want to use 2 in my chart. I am trying this:

var mydata;

// Grab the data
d3.csv('./movies.csv', function(csv){
    csv.forEach(function(d){
        mydata += {rating: d.imdbRating,
                   winsNoms: d.WinsNoms };
    });
});

console.log(mydata);

Can somebody explain exactly what is going on here and why I get undefined when I try to output mydata in the final line? Finally, how do I change it around so it works?

Pob Livsig
  • 95
  • 1
  • 9

2 Answers2

1

You’ll need to first declare an empty array. Then you can push items into it.

To only use part of the set, you can use “slice” to truncate the array.

var mydata = [];

// Grab the data
d3.csv('./movies.csv', function(csv){
    csv.forEach(function(d){
        mydata.push({rating: d.imdbRating,
                   winsNoms: d.WinsNoms });
    });
});

console.log(mydata);
rioV8
  • 24,506
  • 3
  • 32
  • 49
LexJacobs
  • 2,483
  • 15
  • 21
1

You should push objects into an array. See adjustments below:

var mydata = [];

// Grab the data
d3.csv('./movies.csv', function(csv){
    csv.forEach(function(d){
        mydata.push({
            rating: d.imdbRating,
            winsNoms: d.WinsNoms 
        });
    });
});

console.log(mydata);
manish
  • 497
  • 9
  • 17