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

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

May 16, 2016 pm 04:41 PM
json data Traverse

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

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to parse incoming JSON data using request body in FastAPI How to parse incoming JSON data using request body in FastAPI Jul 28, 2023 pm 04:17 PM

How to parse incoming JSON data using request body in FastAPI FastAPI is a modern Python-based web framework that provides rich functionality and high-performance asynchronous support. When using FastAPI to handle HTTP requests, it is often necessary to parse the incoming JSON data. This article will introduce how to use the request body to parse incoming JSON data in FastAPI and provide corresponding code examples. Import dependencies First, we need to import FastAPI dependencies and JS

Java how to loop through a folder and get all file names Java how to loop through a folder and get all file names Mar 29, 2024 pm 01:24 PM

Java is a popular programming language with powerful file handling capabilities. In Java, traversing a folder and getting all file names is a common operation, which can help us quickly locate and process files in a specific directory. This article will introduce how to implement a method of traversing a folder and getting all file names in Java, and provide specific code examples. 1. Use the recursive method to traverse the folder. We can use the recursive method to traverse the folder. The recursive method is a way of calling itself, which can effectively traverse the folder.

How do PHP and MySQL handle JSON data? How do PHP and MySQL handle JSON data? Jul 12, 2023 am 10:00 AM

How do PHP and MySQL handle JSON data? In modern web applications, JSON (JavaScriptObjectNotation) is often used to store and exchange data. PHP and MySQL are two commonly used technologies, and how to use them to process JSON data is a common question. This article will introduce how to use PHP and MySQL to process JSON data and provide some code examples. 1. Export data from MySQL to JSON First, let’s

PHP glob() function usage example: traverse all files in a specified folder PHP glob() function usage example: traverse all files in a specified folder Jun 27, 2023 am 09:16 AM

Example of using PHPglob() function: Traverse all files in a specified folder In PHP development, it is often necessary to traverse all files in a specified folder to implement batch operation or reading of files. PHP's glob() function is used to achieve this requirement. The glob() function can obtain the path information of all files that meet the conditions in the specified folder by specifying a wildcard matching pattern. In this article, we will demonstrate how to use the glob() function to iterate through all files in a specified folder

In-depth comparison of Java Iterator and Iterable: pros and cons analysis In-depth comparison of Java Iterator and Iterable: pros and cons analysis Feb 19, 2024 pm 04:20 PM

Conceptual differences: Iterator: Iterator is an interface that represents an iterator that obtains values ​​from a collection. It provides methods such as MoveNext(), Current() and Reset(), allowing you to traverse the elements in the collection and operate on the current element. Iterable: Iterable is also an interface, representing an iterable object. It provides the Iterator() method, which returns an Iterator object to facilitate traversing the elements in the collection. Usage: Iterator: To use Iterator, you need to first obtain an Iterator object, and then call the MoveNext() method to move to the next

How to use the os module to traverse files in a directory in Python 3.x How to use the os module to traverse files in a directory in Python 3.x Jul 29, 2023 pm 02:57 PM

How to use the os module to traverse files in a directory in Python3.x In Python, we can use the os module to operate files and directories. The os module is an important module in the Python standard library, providing many operating system-related functions. In this article, we will explain how to use the os module to iterate through all files in a directory. First, we need to import the os module: importos Next, we can use the os.walk() function to walk the directory.

Recursively insert and traverse linked list in C++ Recursively insert and traverse linked list in C++ Sep 10, 2023 am 09:21 AM

We get the integer values ​​used to form the linked list. The task is to first insert and then traverse the singly linked list using recursive method. Add node recursively at the end if head is NULL → add node to head otherwise add to head (head → next) recursively traverse nodes if head is NULL → exit otherwise print (head → next) Example input −1-2-7-9 -10 output outputstrong>− linked list: 1→2→7→9→10→NULL input−12-21-17-94-18 output− linked list: 12→21→17→94→18→NULL used in the following program The method is as follows In this method, we will use the function to add nodes and traverse the singly linked list and pass

Java Iterator and Iterable: The key to collection traversal, demystified Java Iterator and Iterable: The key to collection traversal, demystified Feb 20, 2024 am 10:27 AM

Introduction to IteratorIterator is an interface in Java for traversing collections. It provides a set of methods that allow you to access elements in a collection in a sequential manner. You can use Iterator to iterate over collection types such as List, Set, and Map. Demo code: Listlist=newArrayList();list.add("one");list.add("two");list.add("three");Iteratoriterator=list.iterator();while(iter

See all articles