Greetings, I have an array containing various cities:
var cityArr = ["London", "Beijing", "Paris", "New York", "Seoul", "HongKong"];
My goal is to iterate through each city and retrieve JSON data using AngularJS and JavaScript. Here's the code snippet:
for (i=0; i<cityArr.length; i++){
$scope.$watch('fondCity', function () {
cityService.city = $scope.foundCity;
});
var newUrl = "http://api.waqi.info/feed/" + cityArr[i] + "/?token=5336bd836b9ec064f6c6fe0bf7e2781838c15c87";
$http({
method: "GET",
url: newUrl
}).then(function mySucces(response) {
$scope.newData = response.data;
}, function myError(response) {
$scope.newData = response.statusText;
});
}
Now, the question arises: How can I merge all these JSON files into a single array?
Here's an example of a single JSON file:
{
"status": "ok",
"data": {
"aqi": 49,
"idx": 5724,
"attributions": [
{
"url": "http://uk-air.defra.gov.uk/",
"name": "UK-AIR, air quality information resource - Defra, UK"
},
{
"url": "http://londonair.org.uk/",
"name": "London Air Quality Network - Environmental Research Group, King's College London"
}
],
"city": {
"geo": [
51.5073509,
-0.1277583
],
"name": "London",
"url": "http://aqicn.org/city/london/"
}
}
I envision the final merged array looking something like this:
{
"status": "ok",
"data": {
"aqi": 49,
"idx": 5724,
"attributions": [
{
"url": "http://uk-air.defra.gov.uk/",
"name": "UK-AIR, air quality information resource - Defra, UK"
},
{
"url": "http://londonair.org.uk/",
"name": "London Air Quality Network - Environmental Research Group, King's College London"
}
"status": "ok",
"data": {
"aqi": 155,
"idx": 1451,
"attributions": [
{
"url": "http://www.bjmemc.com.cn/",
"name": "Beijing Environmental Protection Monitoring Center (北京市环境保护监测中心)"
},
{
"url": "http://beijing.usembassy-china.org.cn/070109air.html",
"name": "U.S Embassy Beijing Air Quality Monitor (美国驻北京大使馆空气质量监测)"
}
]
{
"status": "ok",
"data": {
"aqi": 28,
"idx": 5722,
"attributions": [
{
"url": "http://www.airparif.asso.fr/",
"name": "AirParif - Association de surveillance de la qualité de l'air en Île-de-France"
}
]