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

Introduction to Javascript Development 3 Array Object Examples_Basic Knowledge

WBOY
Release: 2016-05-16 17:48:21
Original
980 people have browsed it

var a=new Array();
If it is defined like this: a[3]="a"; alert(a.length) 4 is 1,
If it is defined but not assigned, then Return an undefined (alert(a[0]))

Create array:

Copy code The code is as follows:

arr = [];//Yes, an air bracket
//is almost equivalent to the following sentence
arr = new Array() ;var a=new Array(1,"n",3,"m",5,"m",8);
var a=[]; //Define an empty array
var a=new Array(2); //Define an array with a length of 2
var a=[2]; Define an array with an initial value of 2
var a=[1,2,2,2,3,4 ,4,4];

Add and delete elements to the array (push, delete)
Copy code The code is as follows:

var arr=[4545,5456,64646];
arr.push(55,88); //Append two elements to the end of the array
delete arr[2];//Every three elements are deleted directly, but the position is still retained, indicating that the length has not changed, so that we can continue to access the elements at the original position.
Usage of join method in array: Function:
var arr=[1,2,3,4];
alert(arr.join("#")) 1#2#3#4

Improvement of array performance:
Copy code The code is as follows:

var startA=new Date().getTime();
var s=["start"];
for(var i=0;i<999999;i )
{
s.push("ABC");
}
s.join("");
alert(new Date().getTime()-startA);
startA= new Date().getTime();
//var arr=[];
var s="start";
for(var i=0;i<999999;i )
{
s ="ABC";
}
alert(new Date().getTime()-startA);
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