Home Web Front-end JS Tutorial JavaScript Array Example Tutorial

JavaScript Array Example Tutorial

Dec 20, 2016 pm 03:22 PM
javascript

An array is a collection of many variables with the same name. The usage of an array is exactly the same as that of an ordinary variable. It can also be stored in any data type. The only difference is that it occupies a continuous space in the memory. You can number them sequentially and use them accordingly. The previous article [Usage of JavaScript Arrays] mainly introduces the use of js arrays on the Internet. This article introduces the powerful functions and usage methods of js arrays through examples.

Arrays are made up of many variables with the same name. Arrays The usage is exactly the same as that of ordinary variables. It can also be stored in any data type. The only difference is that it occupies a continuous space in the memory. You can number them in sequence and use them according to the number. . The benefit of an array is that it can declare and use multiple variables at once. The method of using JAVAScript arrays is different from that of VBScript. When using it, it should be enclosed in square brackets "[]", and different variables should be separated by commas ",".
var array name;
array name = [1,...,n];
Example: I want to define an array "fruit", which contains three fruits: "watermelon", "apple", and "banana" , we have to write like this:

var fruit;
fruit = ["watermelon", "apple", "banana"]; //Assign values ​​to the three fruits in the array

At this time, "fruit[0]" is " "Watermelon", "fruit[1]" is "apple", "fruit[2]" is "banana", and "fruit" is "watermelon, apple, banana". (JAVAScript will start counting from "0", this principle must be remembered.)
If you want to reassign values ​​to the variables in the array, such as changing "apple" to "strawberry", you should write like this:
fruit = [" Watermelon","Apple","Straw mold"]; //Reassign the variables in the array "fruit"
Because the variables "fruit[0]" and "fruit[2]" in the array remain unchanged, you can also Write like this:
fruit = [fruit[0], fruit[1], "grass mold"]; //Change the variable "fruit[2]" in the "fruit" array to "grass mold"
Note: Even if the variable " The values ​​of "fruit[0]" and "fruit[2]" will not be changed, but they must be written, otherwise the original values ​​will be lost and become "undefined".
In fact, you don’t need to write out all the variables of the array. If you write the array like this:
var fruit;
fruit = ["watermelon", ,"strawberry"]; //Assign values ​​to the three fruits in the array
This When "fruit[0]" is "watermelon", "fruit[1]" is "undefined", "fruit[2]" is "grass mold", and "fruit" is "watermelon,,grass mold".
If you write this array like this:
var fruit;
fruit = ["watermelon", "banana", ,]; //Assign values ​​to the four fruits in the array
At this time, there are four variables in the array "fruit[0 ]" is "watermelon", "fruit[1]" is "straw mold", "fruit[2]" is "undefined", "fruit[3]" is "undefined", "fruit" is "watermelon, straw mold" ,,".
The following is an example:

Note: JAVAScript array and VBScript Arrays are not only different in syntax, but also have many differences when used. Please pay attention to the distinction.

1. Create array object

We will introduce how to create array objects in JAVAScript. (Actually, this method is essentially the same as the previous method, except for the way the statements are written. The above method is simpler to use when the program is very short. In general, I still recommend that you use the following method. Create an array object.) There are two syntaxes for creating an array object:
1. When declaring an array, only declare several components in the array.
var array object name = new Array (number of components);
fruit = new Array(3); //Declare an array named fruit. There are three components in total. This is equivalent to declaring three variables at once
Then you must Prepare a few additional lines of program code and fill in the variable values ​​sequentially.
fruit[0] = "watermelon";
fruit[1] = "apple";
fruit[2] = "banana";
2. Directly give all array components when declaring, separated by commas, Surrounded by parentheses, the number of components is the length of the array.
var array object name = new Array (component one..., component N);
var fruit = new Array ("watermelon", "apple", "banana");
Note: In general languages, the components in the array must are values ​​of the same type, but different types of data can be put into arrays in JAVAScript.

2. Attributes of array objects

JAVAScript provides the following attributes for array objects:
Usage format:
Array object name.Attributes

Ordered attribute name usage instructions
1 constructor specifies the function to create a prototype prototype
2 index represents the index value of the array component
3 input represents the string in the regular expression.
4 length gets the length of the array (number of array components).
5 prototype is used to create custom object properties

3. Methods of array objects

JAVAScript provides the following methods for array objects:
Usage format:
Array object name.Method (parameter)

Ordered method name usage Description
1 concat(array1,array2,…,arrayN) combine multiple arrays into a new array
2 join(separator character) combine arrays into a string, separated by specific characters
3 pop( ) Delete the last component in the array and return the content of the component
4 push (component 1, component 2,..., component N) Add one or more components to the end of the array and return the content of the last component
5 reverse () Reverse the index order of all components in the array (Transpose)
The first component becomes the last one, and the last component is brought to the front
6 shift() Delete the first component in the array and return the Component content
7 slice (start index, end index) Transfer the array content into a new array
8 sort() Sort the array content
9 splice() Add or delete array components
10 toSource() Return the specific array The array constant can be used to create a new array
11 toString() represents the array and its components as a string
12 unshift(component 1, component 2,..., component N) adds one or more components to the array The front, and returns the last array length
13 valueOf() Gets the array value

Note: Some of these methods, such as push, shift, unshift... are not supported by some versions of IE browsers, so you should pay special attention when using them.

Example:

3. Two-dimensional array

JAVAScript’s array object is actually only one-dimensional structure, but we can use the further design and utilization of one-dimensional arrays to put the array into the array, so that the components in the array are also arrays, forming a two-dimensional array in JAVAScript. However, errors are prone to occur in the use of two-dimensional arrays, so we will only introduce its concept here and do not recommend its use. The above is the content of the JavaScript array example tutorial. For more related content, please pay attention to the PHP Chinese website (www .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

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 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 implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

How to use JavaScript and WebSocket to implement a real-time online ordering system How to use JavaScript and WebSocket to implement a real-time online ordering system Dec 17, 2023 pm 12:09 PM

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to implement an online reservation system using WebSocket and JavaScript How to implement an online reservation system using WebSocket and JavaScript Dec 17, 2023 am 09:39 AM

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

How to use insertBefore in javascript How to use insertBefore in javascript Nov 24, 2023 am 11:56 AM

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

JavaScript and WebSocket: Building an efficient real-time image processing system JavaScript and WebSocket: Building an efficient real-time image processing system Dec 17, 2023 am 08:41 AM

JavaScript is a programming language widely used in web development, while WebSocket is a network protocol used for real-time communication. Combining the powerful functions of the two, we can create an efficient real-time image processing system. This article will introduce how to implement this system using JavaScript and WebSocket, and provide specific code examples. First, we need to clarify the requirements and goals of the real-time image processing system. Suppose we have a camera device that can collect real-time image data

See all articles