Javascript 从多个JSON文件创建一个数组

Javascript 从多个JSON文件创建一个数组,javascript,angularjs,arrays,json,Javascript,Angularjs,Arrays,Json,你好,我有很多城市 var cityArr = ["London", "Beijing", "Paris", "New York", "Seoul", "HongKong"]; 我想遍历每个城市,并使用AngularJS和Javascript获得JSON API,以下是我的代码: for (i=0; i<cityArr.length; i++){ $scope.$watch('fondCity', function () { cityService.city =

你好,我有很多城市

var cityArr = ["London", "Beijing", "Paris", "New York", "Seoul", "HongKong"];
我想遍历每个城市,并使用AngularJS和Javascript获得JSON API,以下是我的代码:

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;
        });
    }
我希望它看起来像这样:

{
  "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"
      }
    ],

JSON中的重复键定义

{ “状态”:“确定”, “状态”:“不正常” } 不应使用

您应该将每个JSON回复封装到一个对象中,并且可以将这些对象连接到单个数组中。

var myApp=angular.module('myApp',[]); 函数MyCtrl($scope$http){ var cityar=[“伦敦”、“北京”、“巴黎”、“纽约”、“首尔”、“香港”]; $scope.newData={}; 角度。forEach(城市,函数(值){ var newUrl=”http://api.waqi.info/feed/“+value+”/?令牌=5336bd836b9ec064f6c6fe0bf7e2781838c15c87”; $http({ 方法:“获取”, url:newUrl }).然后(函数mySucces(响应){ $scope.newData[value]=response.data; log($scope.newData); },函数myError(响应){ $scope.newData[value]=response.statusText; }); }); } 期望结果:

[ "London": {"status": "ok", ...}, "Beijing": {"status": ... , ... }, ... ] [ “伦敦”:{“状态”:“ok”,…}, “北京”:{“地位”:…,…}, ... ] 注:

  • 不要污染全局范围,对循环变量使用var关键字
  • 我更喜欢一个内置函数来迭代数组,例如angular.forEach

  • 由于每个响应都具有相同的属性,因此您所能达到的最佳效果就是拥有每个响应的数组

    通过使用,您可以同时使用
    $http
    的承诺

    var promises = cityArr.map(function(name) {
        return $http.get("http://api.waqi.info/feed/" + name + "/?token=5336bd836b9ec064f6c6fe0bf7e2781838c15c87");
    });  // For each City Name, create an array filled with Promises.
    
    // Wait for all the Promises to be completed.
    $q.all(promises).then(function(data) {
        // Create an array of the data attribute from each response.
        var results = data.map(function(result) {
            return result.data;
        });
    
        console.log(results);
    });
    
    您需要添加
    $q
    作为控制器或组件的依赖项


    我创建了一个JSBin示例:

    这实际上不是问题的答案,而是一条注释。在这种情况下,请尝试评论。 var myApp = angular.module('myApp',[]); function MyCtrl($scope, $http) { var cityArr = ["London", "Beijing", "Paris", "New York", "Seoul", "HongKong"]; $scope.newData = {}; angular.forEach(cityArr, function(value) { var newUrl = "http://api.waqi.info/feed/" + value + "/?token=5336bd836b9ec064f6c6fe0bf7e2781838c15c87"; $http({ method: "GET", url: newUrl }).then(function mySucces(response) { $scope.newData[value] = response.data; console.log($scope.newData); }, function myError(response) { $scope.newData[value] = response.statusText; }); }); } [ "London": {"status": "ok", ...}, "Beijing": {"status": ... , ... }, ... ]
    var promises = cityArr.map(function(name) {
        return $http.get("http://api.waqi.info/feed/" + name + "/?token=5336bd836b9ec064f6c6fe0bf7e2781838c15c87");
    });  // For each City Name, create an array filled with Promises.
    
    // Wait for all the Promises to be completed.
    $q.all(promises).then(function(data) {
        // Create an array of the data attribute from each response.
        var results = data.map(function(result) {
            return result.data;
        });
    
        console.log(results);
    });