JavaScript traverses object's property values

Source: Internet
Author: User

Data parsing encountered a small problem, JSON format data multi-layer nesting, probably long so, in fact, the structure is more complex than this
Ex

{"
data": [
    {
        "criminal": [4,0,0,0,0,0,0,0,0,0,0,0],
        "Fire": [0,0,0,0,0,0,0,0,0,0,0,0],
        "Law and Order": [ 2,0,0,0,0,0,0,0,0,0,0,0],
        "Traffic": [3,0,0,0,0,0,0,0,0,0,0,0],
        "help": [2,0,0,0,0,0,0,0,0,0,0,0],
        "other": [ 0,0,0,0,0,0,0,0,0,0,0,0]
    },
    {
      "time": { 
      "date": +,
      "Day": 5,
      "hours":
      " Minutes ":", "
      month": One,
      "seconds": "," Time
      ": 1513925906648,
      " Timezoneoffset ": -480,
      " Year ': 117
      }
  }
  ]
}

Want to traverse to get the array in the object:
In JavaScript, an array can be iterated, but the object is initially traversed with a for loop, showing the undefined;
Such as:

for (Var Key in res.data.data[0]) {
    console.log (key+ ': ' +res.data.data[0]. Key);
}

And the following can be:

   for (Var Key in res.data.data[0]) {
    console.log (key+ ': ' +res.data.data[0][key]);
   }

Another example:

var data ={
      ' Cloudy Day ': ', '
      Sunny ': ' Bayi ',
      ' rainy Day ': ' A '
};
So: for
(var Key in data) {
     Console.log (key+ ': ' +data[key]);
}

The above can be printed on the console:
Cloudy Day: 15
Sunny days: 81
Rainy Days: 12

and the following is not

for (var y in data) {
      Console.log (y+ ': ' +data[y]);
}

Will print on the console:
Cloudy Day: Undefined
Sunny Day: undefined
Rainy Day: undefined

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.