Hi!
I am trying to load and combine data from mulitple URLS from the same API.
For each URL a location summary can be downloaded.
So if I want the summaries from three locations:
var datas = [“https://mwshovel.pythonanywhere.com/dirt/summary/Veveyse/?format=json”,
“https://mwshovel.pythonanywhere.com/dirt/summary/Arabie/?format=json”,
“https://mwshovel.pythonanywhere.com/dirt/summary/Anarchy-Beach/?format=json”
];
Where each URL gives a version of the following:
{ave_dense: “3.9384”, first: “2015-11”, last: “2017-01”, location: “Veveyse”, max_dense: “6.7909”, min_dense: “0.9813”, num_lakes: 1, num_locs: 1, num_rivers: 0, num_samps: 17, seven_five: “4.6791”, stan_dev: “1.5542”, total: 3436, two_five: “2.7360”}
I can get it all in an array like this:
var x_three = ;
datas.forEach(function(url) {
var x_one = d3.json(url);
var x_two = x_one.then(function(data) {x_three.push(data)});
});
Then with console.log(x_three):
0: Object { num_locs: 1, total: 3436, num_samps: 17, … }
1: Object { num_locs: 1, total: 3417, num_samps: 5, … }
2: Object { num_locs: 1, total: 1013, num_samps: 1, … }
length: 3
: Array
All is good up to this point, this is where I don’t get it:
If I call console.log(x_three[0]) from the script: undefined
But console.log(x_three[0]) from the browser console: result as expected
d3.nest() behaves the sameway:
in the script:
d3.nest().key(function(d) {return d.location}).entries(x_three) = undefined
In the browser console: as expected
For now the idea was just to pull the value “element.location” from each result and append to a list.
> <div id="div-five"><ul id="list-two"></ul></div>
> <script>
> var datas = ["https://mwshovel.pythonanywhere.com/dirt/summary/Veveyse/?format=json",
> "https://mwshovel.pythonanywhere.com/dirt/summary/Arabie/?format=json",
> "https://mwshovel.pythonanywhere.com/dirt/summary/Anarchy-Beach/?format=json"
> ];
> var x_three = [];
> datas.forEach(function(url) {
> var x_one = d3.json(url);
> var x_two = x_one.then(function(data) {x_three.push(data)});
> });
> x_three.forEach(function(element){
> console.log(element.location);//this does not work
> });
> console.log(x_three);
> d3.select("#list-two")
> .style("color", "blue")
> .selectAll("li")
> .data(x_three)
> .enter()
> .append("li")
> .append("text").style("color", "red").text((d,[i]) => d[i].location);
> </script>
So how do I do that? Combine the data from multiple URLS and use in a D3 viz and other output for the application?
Thanks, Let me know if this would be better on StackOverflow