Defining arrays
var arr = [];
var arr = new Array ();
var arr = new Array (10); Create an array of length 10
Array manipulation methods
join (): tells the array that all elements are converted to strings and concatenated together, you can specify an optional string to separate the elements of the array, and do not change the original array
var arr = [n/a];
Console.log (Arr.join ("-")); The
Console.log (typeof Arr.join ("-")); String
Console.log (arr); [A]
2. Reserve (): Reverses the order of the elements in the array, returns an array of reverse orders, and changes the original array
var arr = [n/a];
Console.log (Arr.reverse ()); [3,2,1]
3. Sort (): Sorts the elements in the array and returns the sorted array, changing the original array (numbers are from small to large according to the first number, letters are capitalized in alphabetical order before lowercase, undefined at the tail)
var a = [1,3,2];
Console.log (A.sort ()); [A]
Console.log (a); [A]
var arr = ["A", "D", "C"];
Console.log (Arr.sort ()); ["A", "C", "D"]
var arr = [ -3,0,1];
Console.log (Arr.sort ()); [ -3,0,1]
Sort () sorts a number negative 0 positive
Sort () calls the ToString () method when sorting, and then compares it against the ASCII code
Use sort () to sort the numbers in ascending order
var arr = [0,2,1,0.5];
Console.log (Arr.sort (A, b) {
return a-B;
));
Use sort () to sort numbers in descending order
var arr = [0,2,1,0.5];
Console.log (Arr.sort (A, b) {
return b-a;
}));
4. concat () creates and returns a new array without changing the original array
var arr = [n/a];
Console.log (Arr.concat ([up])); [1,2,3,1,2]
Console.log (arr); [A]
5. Slice () returns the specified Subarray, the starting position of the two arguments, the end position, and the default from the beginning to the end of the argument.
do not change the original array
var arr = [1,2,3,4,5];
Console.log (Arr.slice (1,3)); [2,3]
Console.log (arr); [1,2,3,4,5]
6. Splice () method of inserting or deleting elements in an array, changing the original array , the first parameter is the starting position, the second is the number of deleted elements, and the subsequent argument is the inserted element
var arr = [1,2,3,4,5];
Console.log (Arr.splice (2,1));//[3]
Console.log (arr); [1,2,4,5]
Console.log (Arr.splice (2,0,3)); //[]
Console.log (arr); [1,2,3,4,5]
7. push () adds one or more elements at the end of the array, changing the new length of the new array, the original array
pop () removes the last element of the array, changing the last element deleted by the original array .
var arr = [1,2,3,4,5];
Console.log (Arr.push (1));
Console.log (arr);
Console.log (Arr.pop ()); Returns the deleted element
Console.log (Arr.pop ());
8. unshift () add one or more elements at the header of the array, changing the new length of the array, the original array
shift () removes the first element of the array, changing the first element, deleted by the original array
var arr = [1,2,3,4,5];
Console.log (arr.unshift (0)); Returns the new length of the array
Console.log (Arr.shift ()); 0, returns the first element deleted
ECMA5 newly added array operation methods
9. ForEach () iterates through the array, unable to terminate the traversal before all elements are passed to the calling function, with three parameters, an array element, an element index, and a
var data = [1,2,3,4];
Data.foreach (function (v,i,a) {
Console.log (A[i] = v + i); [1,3,5,7];
});
map () calls each element of the array to the specified function and returns an array
var arr = [n/a];
var data = Arr.map (function (x) {return x*x});
Console.log (data); [1,4,9]
The array element returned by filter () is a subset of the call array, and the passed function is used for logical judgment.
var arr = [n/a];
var data = Arr.filter (function (x) {return x < 3;});
Console.log (data); [Up]
every () Some () the array element is applied to the specified function to determine, return TRUE or False
Every () equals, false, which can stop traversing
Some () equals or, if true, can stop traversing
var arr = [1,2,3,4,5];
Console.log (Arr.every (function (x) {
Console.log (x); Output 1, false, stop traversal
return x%2 = = 0;
}));
Console.log (Arr.some (function (x) {
Console.log (x); True when output 1 2,2, stop traversal
return x%2 = = 0;
}));
reduce () reduceright () combines array elements using the specified function, generating a single value, two arguments, first an action function, and a second as the initial value
Reduce () from low index to high index;
Reduceright () from high index to low index;
var arr = [1,2,3,4,5];
Console.log (Arr.reduce (function (x, y) {
Console.log (x);
Console.log (y);
return x + y;
},0)); 15
Console.log (Arr.reduceright (function (x, y) {
Console.log (x);
Console.log (y);
return x + y;
},0)); 15
indexOf () lastIndexOf (), returns the index of the first element searched, without searching to return-1;
var arr = [1,2,3,4,5,1];
Console.log (Arr.indexof (1)); 0
Console.log (Arr.lastindexof (1)); 5
Console.log (Arr.indexof (10)); -1
15. Three ways to detect arrays
Typeof,instanceof,isarray (newly added in E5)
Typeof:typeof arr If an array returns an object
Instanceof:arr instanceof Array returns true for arrays
IsArray:Array.isArray (arr); If the array returns true
var arr = [1,2,3,4,5];
Console.log (typeof arr); Object
Console.log (arr instanceof Array); True
Console.log (Array.isarray (arr)); True
This article is from the "zp1996" blog, make sure to keep this source http://9865481.blog.51cto.com/9855481/1663333
JavaScript Array Manipulation methods