简体   繁体   English

从ajax json请求中,如何将对象动态添加到数组中,以便我可以遍历它们?

[英]From an ajax json request, how can dynamically add the objects to an array so that I can loop through them?

This is what I have so far, essentially I'd like to use the data to instantiate a new info-window from google maps api dynamically from the data response. 到目前为止,这就是我要的东西,本质上,我想使用数据从数据响应中动态地实例化来自Google Maps API的新信息窗口。 I know so far that I'm pushing objects to an array(which are two different data types), but if that's the only wrong here. 到目前为止,我知道我正在将对象推入数组(这是两种不同的数据类型),但是如果这是唯一的错误。 Then how can I dynamically add the response into an object so I can retrieve data with a loop? 然后,如何动态地将响应添加到对象中,以便可以通过循环检索数据?

    var i, venues, allVenues=[]; 
    $.ajax({
    url: 'url',
    dataType: 'json',
    data: 'data'
    async: true,
    success: function(data) {
    venues = data['response']['groups'][0]['items'];
    JSON.parse(venues);
    for(i in venues){
    allVenues.push(venues[i]);
    }
   };
    /*Do something realistic with data other than logging it to console*/
    console.log(allVenues);

You do it right, but not in the right place. 您做对了,但是没有在正确的地方进行。 jQuery.ajax will not wait for the response, but will invoke a 'success' callback when the request is answered. jQuery.ajax不会等待响应,但会在请求得到响应时调用“成功”回调。

Try this: 尝试这个:

var i, venues, allVenues=[]; 
$.ajax({
  url: 'url',
  dataType: 'json',
  data: 'data'
  async: true,
  success: function(data) {
    venues = data['response']['groups'][0]['items'];

    // The following line of code does nothing, because you
    // did not store it's return value. Fortunately it wasn't
    // even needed
    //
    // JSON.parse(venues);

    for(i in venues) {
      allVenues.push(venues[i]);
    }
    // or
    // allVenues.push.apply(allVenues, venues);
    // or in ES6
    // allVenues.push(...venues);
    // or the following will create a new array instance and save it in the allVenues variable
    // allVenues = allVenues.concat(venues);

    /*Do something realistic with data other than logging it to console*/
    console.log("Here are your updated venues:");
    console.log(allVenues);
  }
});
console.log("These are your old venues:");
console.log(allVenues);

EDIT: You can check that the identity of the allVenues array didn't change by printing it to the console every second: 编辑:您可以通过每秒将其打印到控制台来检查allVenues数组的标识是否未更改:

setInterval(function(stored) {
  console.log(stored);
  console.log(stored === allVenues);
}, 1000, allVenues);

EDIT: To update an array to contain only the items of another array, you can use: 编辑:要更新一个数组以仅包含另一个数组的项目,可以使用:

allVenues.length = 0;
allVenues.push.apply(allVenues, venues);

or 要么

allVenues.length = venues.length;
for (var i in venues) {
  allVenues[i] = venues[i];
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何访问通过 ajax 发送到 php 文件的 POST 请求的 JSON 数据并将它们存储在数组变量中? - how can i acces the JSON data of POST request sent through ajax to a php file and store them in an array variable? 如何遍历div数组以显示()? - How can I loop through an array of divs to show() them? Javascript-如何将一个数组中的点添加到另一个数组中-所以我可以将它们相加并比较两个数组 - Javascript - how to add points from one array to another - so I can tally them up and compare the two 我该如何打开代理对象,以便将它们序列化为Json? - How can I turn de-proxy objects so that I can serialize them into Json? 如何遍历api数组中的多个对象 - How can I loop through multiple objects in a api array 如何遍历 javascript 中的这个对象数组? - How can I loop through this array of objects in javascript? 如何遍历数组中的所有数字,然后加上或减去它们,使它们达到预定值 - How can you iterate through all the numbers in an array and either add or subtract them so they hit a predetermined value 如何通过ajax将一个对象数组从javascript传递到asp.net-mvc函数? - How can i pass an array of objects from javascript to asp.net-mvc function through ajax? 如何在从 html 集合创建的数组中通过 for 循环从对象中获取元素的值? - How can I get values of elements from objects through for loop in an array created from a html collection? 我有一些 class 的实例,我将它们放入一个数组中。 我可以遍历数组并更新所有数组对象的价格吗? - I have some instances of a class and I pushed them into an array. Can I loop through the array and update the price of all the array objects?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM