Home > Web Front-end > JS Tutorial > body text

Create JSON data directly in JS and then traverse using _javascript techniques

WBOY
Release: 2016-05-16 16:41:10
Original
1112 people have browsed it

I have already talked about returning json data to the foreground in the background and traversing the json data in the foreground.

Here we will talk about creating JSON data directly in JS, and then traversing and using it~

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

var YearSelect = {}; 
var Year = 2014; 
var DateOption; 
for (var i = Year; i < Year + 12; i++) { 
DateOption = {'Year':i, 'Month':i-Year+1}; 
/ alert(DateOption.Year) 
YearSelect[i] = DateOption; 
}
Copy after login

Here is a JSON object created, including year and month data.

Why I created JSON objects is because I am familiar with JSON objects. What the php background returns is also a json object.

json object has no length attribute~~

So to traverse:

for(var key in YearSelect){ 
alert(YearSelect[key].Year); 
alert(YearSelect[key].Month); 
}
Copy after login

That’s it~

Remember to distinguish between json objects and arrays~ otherwise it will always be undenfined

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!