简体   繁体   English

如何遍历JavaScript数组/对象

[英]How to traverse a javascript array/object

I am having a hard time trying to get my data from this array. 我很难从该数组中获取数据。 Can someone please tell me how to do this? 有人可以告诉我该怎么做吗?

I need to get the latLng and AdminArea4 from within the locations for an autocomplete. 我需要从locations获取latLngAdminArea4以获得自动完成功能。

Can someone please explain the difference between the {} and [] . 有人可以解释{}[]之间的区别吗? From what I know, one is an array and the other an object but I am not sure if this is right. 据我所知,一个是数组,另一个是对象,但是我不确定这是否正确。 Is the process different from traversing an array than an object? 这个过程与遍历数组而不是遍历对象不同吗?

{
   "results": [
      {
         "locations": [
            {
               "latLng": {
                  "lng": 24.873108,
                  "lat": 59.389755
               },
               "adminArea4": "Harju maakond",
               "adminArea5Type": "City",
               "adminArea4Type": "County",
               "adminArea5": "Rae vald",
               "street": "",
               "adminArea1": "EE",
               "adminArea3": "",
               "type": "s",
               "displayLatLng": {
                  "lng": 24.873108,
                  "lat": 59.389755
               },
               "linkId": 0,
               "postalCode": "",
               "sideOfStreet": "N",
               "dragPoint": false,
               "adminArea1Type": "Country",
               "geocodeQuality": "CITY",
               "geocodeQualityCode": "A5XXX",
               "adminArea3Type": "State"
            },
            {
               "latLng": {
                  "lng": 24.860184,
                  "lat": 59.38493
               },
               "adminArea4": "Harju maakond",
               "adminArea5Type": "City",
               "adminArea4Type": "County",
               "adminArea5": "Rae",
               "street": "",
               "adminArea1": "EE",
               "adminArea3": "",
               "type": "s",
               "displayLatLng": {
                  "lng": 24.860184,
                  "lat": 59.38493
               },
               "linkId": 0,
               "postalCode": "",
               "sideOfStreet": "N",
               "dragPoint": false,
               "adminArea1Type": "Country",
               "geocodeQuality": "CITY",
               "geocodeQualityCode": "A5XXX",
               "adminArea3Type": "State"
            },
         ]
      }
   ]
}

First some explanation about arrays and objects since you've asked and it's quite relevant to how to use the data structure you have: 自您提出要求以来,首先对数组和对象进行一些说明,这与如何使用您拥有的数据结构非常相关:

{} defines an object. {}定义一个对象。 [] defines an array. []定义一个数组。

An object is a unordered list of property/value pairs with no property name repeated. 对象是属性/值对的无序列表,没有重复的属性名称。

An array is an ordered list of individual items with a .length property that tells you how many items are in the array. 数组是具有.length属性的单个项目的有序列表,该属性告诉您数组中有多少个项目。


An object would be defined like this: 对象的定义如下:

var obj = {
   prop1: value1,
   prop2: value2
};

And, you can reference values like this: 而且,您可以参考如下值:

console.log(obj.prop1);    // value1

An array is defined like this: 数组的定义如下:

var myArray = ["one", "two", "three"];

And, you can access elements by zero-based index like this: 而且,您可以通过基于零的索引访问元素,如下所示:

console.log(myArray[0]);   // "one"

Or, you can iterate all the items in an array like this: 或者,您可以像这样遍历数组中的所有项目:

for (var i = 0; i < myArray.length; i++) {
    console.log(myArray[i]);
}

Assuming your overall data structure is in a variable named data , you can iterate the results and nested locations arrays like this: 假设您的整体数据结构位于名为data的变量中,则可以迭代结果和嵌套的位置数组,如下所示:

var results = data.results;
var locations;
for (var j = 0; j < results.length; j++) {
    locations = results[j].locations;
    for (var i = 0; i < locations.length; i++) {
       console.log(locations[i].latLng);
       console.log(locations[i].adminArea4);
    }

To explain: 解释:

  1. data.results is an array that you need to iterate. data.results是您需要迭代的数组。
  2. In each element of that array is an object with a locations property in it. 在该数组的每个元素中都有一个带有locations属性的对象。
  3. The locations property is another array that you need to iterate. locations属性是您需要迭代的另一个数组。
  4. In each element of the locations array is an object with a latLng property and a adminArea4 property on it. 在locations数组的每个元素中都是一个对象,该对象上具有latLng属性和adminArea4属性。

It's json structure.. You can access elements as I mentioned below.. Assign json to variable and then access object properties. 它是json结构。您可以访问下面提到的元素。将json分配给变量,然后访问对象属性。

var obj = {
   "results": [
      {
         "locations": [
            {
               "latLng": {
                  "lng": 24.873108,
                  "lat": 59.389755
               },
               "adminArea4": "Harju maakond",
               "adminArea5Type": "City",
               "adminArea4Type": "County",
               "adminArea5": "Rae vald",
               "street": "",
               "adminArea1": "EE",
               "adminArea3": "",
               "type": "s",
               "displayLatLng": {
                  "lng": 24.873108,
                  "lat": 59.389755
               },
               "linkId": 0,
               "postalCode": "",
               "sideOfStreet": "N",
               "dragPoint": false,
               "adminArea1Type": "Country",
               "geocodeQuality": "CITY",
               "geocodeQualityCode": "A5XXX",
               "adminArea3Type": "State"
            },
            {
               "latLng": {
                  "lng": 24.860184,
                  "lat": 59.38493
               },
               "adminArea4": "Harju maakond",
               "adminArea5Type": "City",
               "adminArea4Type": "County",
               "adminArea5": "Rae",
               "street": "",
               "adminArea1": "EE",
               "adminArea3": "",
               "type": "s",
               "displayLatLng": {
                  "lng": 24.860184,
                  "lat": 59.38493
               },
               "linkId": 0,
               "postalCode": "",
               "sideOfStreet": "N",
               "dragPoint": false,
               "adminArea1Type": "Country",
               "geocodeQuality": "CITY",
               "geocodeQualityCode": "A5XXX",
               "adminArea3Type": "State"
            },
         ]
      }
   ]
};

Travsering Values 旅游价值

obj.results[0].locations[0].latLng.lng
obj.results[0].locations[0].latLng.lat

You can also traverse by using for loop. 您也可以使用for循环遍历。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM