0

I've created an array of objects from a csv file so:

var s=[];
d3.csv('income.csv', function(data) {
  data.forEach(function(d, i) {
    w={};
    w.geoID= data[i]['GEO.id2'];
    w.population = data[i]['HC01_EST_VC15'];
    s.push(w);
  })
})

console.log(s); 
console.log(s[1].geoID);

console.log(s) reveals:

[]
0: Object { geoID: "Id2", population: "Households; Estimate; Mean income (dollars)" }
1: Object { geoID: "37001", population: "59084" }
2: Object { geoID: "37003", population: "52750" }
3: Object { geoID: "37005", population: "47715" }
4: Object { geoID: "37007", population: "45101" }
5: Object { geoID: "37009", population: "50702" }
6: Object { geoID: "37011", population: "56130" }

However, console.log(s[1].geoID) reveals:

undefined

What gives - aren't i merely calling the geoID key in index 1 of this array called 's'?

mac
  • 318
  • 4
  • 16
  • **where** are you doing the console.log? Inside the `d3.csv` callback? Besides that: your code seems to be unnecessary. Just use a row conversion inside the `d3.csv`. – Gerardo Furtado Mar 18 '18 at 22:35
  • That depends on where (and when) you're trying to access `s`. Possibly "[Why is my variable unaltered after I modify it inside of a function?](https://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron)" – Jonathan Lonowski Mar 18 '18 at 22:35
  • outside the callback - both console.log(s) and console.log(s[1].geoID) were run in the same place – mac Mar 18 '18 at 22:37
  • So, both should be `undefined`. `d3.csv` is asynchronous, so anything depending on `data` should be inside the callback. The only reason why you may not see `undefined` for some console.log is that it's a reference which can change. – Gerardo Furtado Mar 18 '18 at 22:37
  • both are not undefined - only one is – mac Mar 18 '18 at 22:39
  • @mac `s` should be defined, but it'll remain empty outside of the callback. When using asynchronous functions, like `d3.csv()`, the order of execution doesn't strictly follow the lines of your code. – Jonathan Lonowski Mar 18 '18 at 22:42
  • ~ and the suggested links don't go far in answering this question. s as a whole can be accessed, so surely its parts should be accessible – mac Mar 18 '18 at 22:45
  • @mac Yes, it does answer. `console.log` is a **live** reference. The moment you try to `console.log(s)` it is an **empty** array. Just some time later it is an array with objects. See here: https://bl.ocks.org/anonymous/d81138fdb8ecefd35af24401681f423a/d672d295367b142d9653f5e9cdf9839846221b29 That's the default behaviour of modern browsers, the value of `console.log` is **not** the value when you called it, but the recent value of the object. – Gerardo Furtado Mar 18 '18 at 22:48
  • Try `console.dir` and you'll see that it is an empty array **at the moment the code runs**. You'll see something like `Array(0)`. – Gerardo Furtado Mar 18 '18 at 22:48
  • yes, bl.ocks code replicates my issue - i'll look into it a bit further - thanks. – mac Mar 18 '18 at 22:52
  • @mac No worries. Besides that issue, remember what I said: you don't need that code with an empty array and `push`. Just use a row conversion to change `data`. – Gerardo Furtado Mar 18 '18 at 22:54
  • I would post my solution - but it seems this page doesn't provide a means... – mac Mar 20 '18 at 00:39

0 Answers0