将数据推送到阵列,然后迭代

Push data to array and then iterate?

本文关键字:然后 迭代 阵列 数据      更新时间:2023-09-26

我正在尝试在for语句中获取一堆数据,将其推送到对象数组,然后将其提取?

var d = {
    city: [{
       region: '',
       name: '',
       type: ''
    }]
}

我从cityList[i]那里获取数据 - 每个大约有 10 个 - 比如

for(var i=0, city; i < cityList.len; i++ {
      city = cityList[i]
     //manipulate a bit 
    d.city.push(data, data1, data2) //i.e. region, name, type
}

然后我想显示来自d.city的数据?我该怎么做

将数据设置为数组应如下所示:

d.city.push({
   region: data,
   name: data2,
   type: data3
});

要从d.city检索数据,您可以使用:

$.each(d.city, function() {
  console.log(this.region);
  console.log(this.name); // etc
});