Create JSON data directly in JS and iterate through the use of _javascript techniques

Source: Internet
Author: User

Before I've talked about the back of the table returns JSON data to the foreground and traverses the JSON data in the foreground.

Here, directly in JS to create JSON data, and then traverse the use of ~

The creation code is as follows: (a JSON object is created)

var yearselect = {}; 
var year = 2014; 
var dateoption; 
for (var i = year; I < year + i++) { 
dateoption = {' Year ': I, ' Month ': i-year+1}; 
/alert (dateoption.year) 
yearselect[i] = dateoption; 
}

Here is the creation of a JSON object that includes the year and month data.

Why I created the JSON object is because I'm familiar with the JSON object. The back of the PHP backend is also a JSON object.

JSON object has no length attribute ~ ~

So traverse the words to:

for (var key in Yearselect) { 
alert (Yearselect[key].) year); 
Alert (Yearselect[key). Month); 
}

So it's OK.

Keep in mind that you have to distinguish between the objects and arrays of JSON, or it's always undenfined.

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.