Home  >  Article  >  Web Front-end  >  About the use of JavaScript Array (array) objects

About the use of JavaScript Array (array) objects

jacklove
jackloveOriginal
2018-05-07 09:45:041545browse

The function of the array object is to use separate variable names to store a series of values, which has an important role. The detailed explanation is as follows:

var mycars = new Array();
mycars[0] = "Saab";
mycars[1] = "Volvo";
mycars[2] = "BMW";

You can find more examples at the bottom of the page.

What is an array?

An array object uses separate variable names to store a series of values.

If you have a set of data (for example: car name), there are separate variables as follows:

var car1="Saab";
var car2="Volvo";
var car3="BMW";

However, what if you want to find a certain car from it? And not 3 cars, but 300 cars? This will not be an easy task!

The best way is to use an array.

Arrays can store all values ​​with one variable name, and any value can be accessed with the variable name.

Each element in the array has its own ID so that it can be easily accessed.

Create an array

There are three ways to create an array.

The following code defines an array object named myCars:

1: Regular way:

var myCars=new Array(); 
myCars[0]="Saab";       
myCars[1]="Volvo";
myCars[2]="BMW";

2: Concise way:

var myCars=new Array("Saab","Volvo","BMW");

3 : Literal:

var myCars=["Saab","Volvo","BMW"];

Accessing the array

You can access a specific element by specifying the array name and index number.

The following example can access the first value of the myCars array:

var name=myCars[0];

The following example modifies the first element of the array myCars:

myCars[0]="Opel";




[0] is the first element of the array. [1] is the second element of the array.



You can have different objects in an array

All JavaScript variables are objects. Array elements are objects. Functions are objects.

So, you can have different variable types in the array.

You can include object elements, functions, arrays in an array:

myArray[0]=Date.now;myArray[1]=myFunction;myArray[2]=myCars;

Array methods and properties

Use predefined properties and methods of array objects:

var x=myCars.length             // myCars 中元素的数量
var y=myCars.indexOf("Volvo")   // "Volvo" 值的索引值

Complete Array Object Reference Manual

You can refer to this site’s complete reference manual for all properties and methods of arrays.

The reference manual contains descriptions of all properties and methods (and more examples).

Complete Array Object Reference Manual

Creating new methods

The prototype is the JavaScript global constructor. It can construct properties and methods of new Javascript objects.

Example: Create a new method.

Array.prototype.myUcase=function(){    
for (i=0;i<this.length;i++){        
this[i]=this[i].toUpperCase();   
 }
 }

The above planning department article explains how to use JavaScript Array (array) objects. For more detailed content, please pay attention to the php Chinese website to watch and learn.

Related recommendations:

Several common JS sorting codes on the front end

Learn more about the basic content of JavaScript Array objects

About how to use jQuery stop()

The above is the detailed content of About the use of JavaScript Array (array) objects. For more information, please follow other related articles on the PHP Chinese website!

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