js array of traditional methods
Function: Add one or more elements to the end of an array
var arr = [4];
arr.push(1,2,3); // [4,1,2,3]
Returns: returns the new length of the array length
Function: delete the last element of the array
var arr = [1,2,3,4];
arr.pop(); // [1,2,3]
The new array will be deleted after return: the return
Function: adding one or more elements to the beginning of an array
var arr = [1,2,3,4];
arr.unshift(7); // [7,1,2,3,4]
Returns: returns the new length of the array length
Function removes the first element of the array
var arr = [1,2,3,4];
arr.shift(); // [2,3,4]
The new array will be deleted after return: the return
Array traversal:
function Person(name,age) {
this.name = name ;
this.age = age;
};
var per1 = new Person('a',12),
per2 = new Person('b',14),
per2 = new Person('c',11),
perArr = [per1,per2,per3];
function getAdult(arr){
var newArr = [];
for(var i=0;i<arr.length;i++) {
if(arr[i]['age']>12) {
newArr['push'](arr[i]);
}
}
return newArr;
};
Function: extracting elements from an array
parameters:
a: the start position taken index
2: index of the end position, taken
second parameter can be omitted, showing the beginning of the start from the back were taken
(no header trailer)
second parameters can also be passed from the start of a negative value indicates the number of
var result = array.slice(start,end);
var arr = [1,2,3,4];
var newArr = arr.splice(1,-1); //[2,3] -1代表4
The new array will not affect the original list, returns after completing the interception Returns:
Function: Delete the specified array element or additional element is added to the specified location
parameters:
1: represents the start position of the index
2: represents the number of the deleted
3: These elements can tell automatically inserted in front of the index to the starting position
var arr = [1,2,3,4,5];
arr.splice(0,2,1212); //[1212,3,4,5]
arr.splice(0,0,1212121); //往第一个参数的前面添加元素 [1212121,1,2,3,4,5]
Deduplication Array:
var arr = [1,2,2,3,4,4,3,4,4,5,5,7];
for (var i = 0; i < arr.length; i++) {
for (var j = 1+i; j < arr.length; j++) {
if(arr[i]===arr[j]) {
arr.splice(j,1);
j--; //注意删了一位后,后面的数自动往前推一位,导致相邻的一位没有被比较
}
}
}
console.log(arr);
Function: connecting two or more arrays
var arr = [1,2];
var arr1 = [22,33];
var newArr = arr.concat(arr1);
The new array will be returned after the link Returns:
Function: a character string converted into an array, the array will not affect the original
parameters: a string argument can be specified, the string will become the connector elements in the array, the default is ',' l connector
var arr = [1,2];
var result = arr.join(); //'1,2'
var result = arr,join(''); //'12'
Returns: returns a new array
Function: Reverse Array
var arr = [1,2,3,4];
arr.reverse(); // [4,3,2,1]
The new array will be returned after the reverse: Return
Function: array sort, can affect the original array, default sorted by unicode encoding, from small to large
Parameters: callback function
uses the element in the array as an argument to the callback function, respectively,
the position will be determined elements based on the return value of the callback function
If the return position of the switching element is greater than 0.
If a return is less than the element 0 position remains unchanged
when a return to the element position constant == 0
var arr2 = [1,4,5,6,3,65,11];
arr2.sort(function(a,b) {
if(a>b) {
//前边大 换位置(升序)
return 1;
} else if(a<b){
return -1;
} esle {
return 0;
};
//return a-b; 同上(升序)
//return b-a;(降序)
});
Function: the array into a string
var arr = [1,2,3,4];
arr.toString(); // "1,2.3,4"