【问题标题】:Code not adding object to an array代码未将对象添加到数组
【发布时间】:2011-10-30 18:44:35
【问题描述】:

我正在尝试将从 JSON 文件中检索到的一组对象添加到数组中。我试过使用push,但结果是一样的。数组的length 值仍为0。我究竟做错了什么? JSON 解析得很好,因为我可以在循环期间获取值。

<script type="text/javascript"> 

//my array
var myArray = new Array();

function performSearch(){

    var url = "http://myjsonurl...";
    var counter = 0;

    $.getJSON(url, function(response){
         $.each(response.data.people, function() {

            //p is the the object to add to the array
            var p = new person(this.name, this.age);

            //tried using myArray.push instead of having a counter, but
            //I get the same length of 0.
            myArray[counter] = p;
            counter++;
        });

    });     

    //always returns 0  
    alert(myArray.length);

}

... 
</script>

【问题讨论】:

    标签: javascript arrays json push


    【解决方案1】:

    getJSON() 是一个异步函数。它仅在您调用它时才开始获取数据,并且仅在加载给定函数后才调用它。因此,您在获取任何内容之前调用警报。您应该在 .each() 函数之后立即收到警报。

    【讨论】:

      【解决方案2】:

      Ajax 是异步的。任何依赖于 JSON 的事情都需要发生在回调中。

      function performSearch()
      {
          var url = "http://myjsonurl...";
          $.getJSON(url, function(response)
          {
              var myArray = $.map(response.data.people, function()
              {
                  return new person(this.name, this.age);
              });
      
              alert(myArray.length);
          });     
      
          //always returns 0  
          alert(myArray.length);
          // that's because this code executes before the $.getJSON callback does
      }
      

      【讨论】:

        【解决方案3】:

        在将对象放入其中之前,您将返回数组。

        getJSON 方法中使用的回调函数不会立即运行,它会在响应到达时运行。由于两个方法不能同时运行,所以在回调函数运行之前,你总是会退出你的函数。

        您可以在回调函数中访问结果:

        <script type="text/javascript"> 
        
        function performSearch(){
        
          var url = "http://myjsonurl...";
        
          $.getJSON(url, function(response){
        
            var myArray = [];
        
            $.each(response.data.people, function() {
              var p = new person(this.name, this.age);
              myArray.push(p);
            });
        
            alert(myArray.length);
        
          });     
        
        }
        
        </script>
        

        【讨论】:

          【解决方案4】:
          <script type="text/javascript"> 
          
          //my array
          var myArray = new Array();
          var counter = 0;
          
          function performSearch(){
          
              var url = "http://myjsonurl...";
          
              $.getJSON(url, function(response){
                   $.each(response.data.people, function() {
          
                      //p is the the object to add to the array
                      var p = new person(this.name, this.age);
          
                      //tried using myArray.push instead of having a counter, but
                      //I get the same length of 0.
                      myArray[counter] = p;
                      counter++;
          
                      alert(myArray.length);
          
                  });
          
              });     
          
          
          }
          
          ... 
          </script>
          

          【讨论】:

          • 假设你需要多次调用 performSearch 函数。
          猜你喜欢
          • 2022-01-13
          • 2018-09-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多