1. String Conversions to arrays
Split ();
Console.log ("AABBCC". Split (")"); Print: ["A", "a", "B", "B", "C", "C"]
2. Array conversion to string
Join ();
Console.log ([' AA ', ' BB ', ' VV '].join (")"); Print: AABBVV
3. Finding elements in an array
IndexOf (); Used to find whether an element is in an array, returns an index of an array if it exists, or returns-1;
["AA", ' BB '].indexof (' BB '); return "1"
The indexOf () function always returns the first index of the same element as the parameter;
The LastIndexOf () method, which returns the index of the last element in the same element, and returns 1 if it is not found
["AA", ' BB ', ' cc ', ' AA '].lastindexof (' AA '); 3["AA", ' BB ', ' cc ', ' AA '].indexof (' AA '); 0
4. Array merging
Concat ();
Console.log (["AA", ' BB '].concat ([' cc ')]); return ["AA", "BB", "CC"];
5. Capturing a new array from an existing array
Splice (A, b); The first parameter of the method is the starting index of the Intercept, and the second argument is the length of the Intercept
["AA", "BBB", "VVV", "ddd", "ee"].splice (2,2) return ["VVV", "ddd"]
6. Returning the selected element from an existing array
Slice ();
Arrs.slice (Start,end);
Start must specify where to start the selection, and if it is a negative number, specify the position from the end of the array, such as:-1 is the last element, and 2 is the second-lowest element.
End is optional, specifying where to end the selection, which is the array subscript at the end of the array fragment, and if no parameter is specified, the segmented array contains all elements from start to end of the array.
[' AA ', ' BB ', ' cc ', ' DD '].slice; return ["BB"];
7. Add an element from the last array
Push ();
var nums = [1,2,3,4,5];nums.push (6); Console.log (nums); [1,2,3,4,5,6];
8. Add one or more elements to the beginning of the array, returning the new length
Unshift ();
var arrs = [1,2,3,4,5];console.log (Arrs.unshift ("AA", "BB")); 7console.log (ARRS); ["AA", "BB", 1, 2, 3, 4, 5]
9. Delete the element at the end of the array to return the deleted element
Pop ();
var arrs = [1,2,3,4,5,6];console.log (Arrs.pop ()); 6console.log (ARRS);//1,2,3,4,5
10. Delete the first element of an array
Shift (); (Remember: Shift is the first to delete the deleted element, Unshift is to add the element to the first return total length)
var nums = [9,1,2,3,4,5,6];console.log (Nums.shift ()); 9console.log (nums);//[1,2,3,4,5,6];
11. Adding and removing elements from an array's middle position
Inserts an element in the middle of an array. var nums = [1,2,3,7,8,9];nums.splice (3,0,4,5,6); Console.log (nums);//[1,2,3,4,5,6,7,8,9]
Removes an element from the array. var nums = [1,2,3,100,200,300,400,4,5];nums.splice (3,4); Console.log (nums);//1,2,3,4,5
12. Array sorting
Reverse ();
Flips the order of elements in an array
var nums = [1,2,3,4,5];nums.reverse (); Console.log (nums);//5,4,3,2,1
If it is a string sort, you can use sort ();
var arrs = ["Da", "Ade", "CDF", ' BFG '];arrs.sort (); Console.log (arrs);//["Da", "Ade", "BFG", "CDF"]
If the array element is a numeric type, then there is a problem.
var nums = [3,1,2,100,4,200];nums.sort (); Console.log (nums);//[1, 100, 2, 200, 3, 4]
The sort () method sorts the elements in a dictionary order, so he assumes that the elements are string types. But for the digital type we can simply write a function to
function Compare (num1,num2) { return num1–num2;}
var nums = [3,1,2,100,4,200];
Num.sort (Compare);
Console.log (nums);//1,2,3,4,100,200
and some new elements.
ForEach (), every (), some (), reduce (), reduceright (), map (), filter (). It's for standard browsers.
Ie9+,firefox,chrome operation, IE8 and below are not supported.
ForEach () takes a function as an argument and uses the function for each element of the array
function Square (num) {console.log (num*num);} var nums = [1,2,3,4,5,6,7,8,9,10];console.log (Nums.foreach (square));//1,4,9,16,25,36,49,64,81,100
Every () accepts a function that returns a Boolean type, uses the function for each element in the array, or True if all returns True, otherwise false
.... No longer introduced
Excerpt from the network
Array Common methods