JavaScipt array Daquan

JavaScript study, compiled some knowledge of the array, we want to help, please exhibitions Great God!

First, declare an array of

1, by creating new array, any parameter passing
2 is omitted to create new array
3, array constants assignments via the
three forms of the above are:

var arr = new Array(100,true,"hello");       
var arr = Array(100,true,"hello");       
var arr = [100,true,"hello"];

Meanwhile, it is noted, both methods 1 and 2, passing only one parameter, but numbers when it is declared an array of length 10; and the third method only when passing a parameter, and a digital time, is only one array elements, elements that number.

Second, iterate

The return value is an array name .length array element number
1, for loop

var arr = [100,true,"hello"];
for(var i = 0; i < arr.length; i++){          
	document.write(arr[i] + "</br>")
	}

2, for ... in traversal

var arr = [10,20,30,40,50];       
//在页面上分别将每一个数输出       
for(var i in arr){           
	document.write(arr[i] + "<br/>"); 
      }

Third, the method of the array

1, the structure of the stack
structure: head feed from the same, from the same head
characteristics: an advanced
two array-formed stack structure:
Push Method Format: .push array (parameter 1, parameter 2 ......)
Function: to added elements of the array at the end of
the return value: after completion of the plug element of the array length

var arr = ["北京","上海","广州"];       
var res = arr.push("深圳","西安");       
alert(arr); //输出北京,上海,广州,深圳,西安
alert(res); //输出5

pop method Format: .pop Array ()
Parameters: No Parameter
Return Value: a removable element
functions: removing an element from the end of the array

var arr = ["北京","上海","广州"];      
var res = arr.pop();       
alert(res);//广州       
alert(arr);//北京,上海

2, the structure of the queue
structure: feed from the end, from the head of the
features: FIFO
two methods for forming arrays queue structure:
Shift method:
Format: Array .shift ()
Parameters: No parameters
Function: the array from the head the next element
return value: a removable element

var arr = ["北京","上海","天津"];        
var res = arr.shift();        
alert(res);//输出北京        
alert(arr);//输出上海,天津

unshift Method:
Format: Array .unshift (parameter 1, parameter 2 ......)
Function: insert elements from the head array
Return Value: after completion of insertion elements of the array length

var arr = ["北京","上海","广州"];
var res = arr.unshift("天津");
alert(res);//4
alert(arr);//北京,上海,广州,天津

3, contact method of
action:
A, array copy of the original, generates a new array
B, merge array
format: .contact array name (or data array)
Return value: new array

var arr1 = [10,20,30];        
var arr2 = [50,60,70];        
var newArr = arr1.concat(arr2,"hello",true);        alert(newArr);//10,20,30,50,60,70,hello,true        
alert(newArr.length);//8       
alert(arr1);//10,20,30

Thus, the method does not change the original array Contact
4, slice method
role: designated area may be obtained based on the current element array
[start, end] generating a new array element is extracted in the region
Return value: an array of newly generated, the original array constant

var arr = [10,20,30,40,50,60];
var newArr = arr.slice(1,4);
alert(newArr);//20,30,40  
alert(arr);//10,20,30,40,50,60

5, splice method
Format: array name .splice (start, length, data ......)
Start: start position taken
length: number of taken
a, add
a return value: the element array of the taken

var arr = [10,20,30,40,50];  
var newArr = arr.splice(2,0,"hello","world");//截取0个元素,达到添加元素的效果        
alert(arr);//10,20,hello,world,30,40,50        
alert(newArr);//空数组

b, deleted
Return Value: intercepting element in an array

var arr = [10,20,30,40];
//从第一个元素开始删除两个元素
var res = arr.splice(1,2);        alert(arr);//10,40        
alert(res);//20,30

C, modified (deleted after the first increase, the effect of modification is formed in the visual element)

var arr = [10,20,30,40];
var newArr = arr.splice(2,1,"hello");
alert(arr);//10,20,hello,40

6, join method
Format: Name Array .join
effect: the elements of the array with the incoming symbol splicing, splicing into a string
Return Value: string splicing

var arr = [10,20,30,40];        
var str = arr.join("==");
alert(str);// 10==20==30==40       
alert(arr);// 10,20,30,40

7, reverse method
Format: array name .reverse
effect: reverse

var arr = [true,"hello",100];        
arr.reverse();        
alert(arr);

8, sort method
Format: array name .sort
role: Sort

//注:按字符串大小比较
var arr = [30,10,40,20];
arr.sort();        
alert(arr);

If you want value from small to large

var arr = [30,10,50,20,60];        
arr.sort(function(value1, value2){            
return value1 - value2;        
});        
alert(arr);//10,20,30,50,60

If the descending comparison, the order of the above code value1 - value2 to value2 - value1 can.

Released four original articles · won praise 6 · views 78

Guess you like

Origin blog.csdn.net/qq_46127363/article/details/105071047