score:1

Accepted answer

you want to iterate over the keys in data, use the key to look up the corresponding value in data, and operate on the values.

you want something like this:

d3.json("data/data2.json", function(error, data) {
    for (k in data) {
        var k_data = data[k];
        k_data.foreach(function(d) {                              
            d.date = parsedate(d[0].date);                          
            d.close = +d[0].close;                               
        });
    }
});

also, it looks like foreach takes a function that has two arguments, key and value:

foreach: function(f) {
  for (var key in this) {
    if (key.charcodeat(0) === d3_map_prefixcode) {
      f.call(this, key.substring(1), this[key]);
    }
  }
}

for example:

values: function() {
  var values = [];
  this.foreach(function(key, value) {
    values.push(value);
  });
  return values;
}

later: ameliabr is correct about foreach: it is not available for use on objects/dictionaries.

var a = {"stock1": [1, 2, 3, 4], "stock2": [2, 3, 5, 7], "stock3": [1,2, 4,8]};
a.foreach(function(value, key){ console.log(value, key);});
/* typeerror: object #<object> has no method 'foreach' */

but this works:

a["stock1"].foreach(function(value, key){ console.log(value, key);});
1 0
2 1
3 2
4 3

Related Query