Home  >  Article  >  Web Front-end  >  Create JSON data directly in JS and then traverse using _javascript techniques

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

WBOY
WBOYOriginal
2016-05-16 16:41:101128browse

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; 
}

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); 
}

That’s it~

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

Statement:
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