- Add or remove an element at the end of an array: Push () and pop ()
Push (): Appends one or more elements to the tail of the array and returns the length of the array
Pop (): Deletes an element at the end of the array and returns the deleted item
1 vararr = ["AAA", "BBB", "CCC"];2 varPushres = Arr.push ("ddd", "Eee");3Console.log (Pushres);//5 (returns the length of the new array)
4Console.log (arr);//["AAA", "BBB", "CCC", "ddd", "Eee"]
5 varpopres=Arr.pop ();
6Console.log (Popres);//Eee
7Console.log (arr);//["AAA", "BBB", "CCC", "DDD"]
- Add or remove an element to the head of an array: Unshift () and Shift ()
Unshift (): Inserts one or more elements in the head of the array and returns the length of the array
Shift (): Deletes an element in the head of the array and returns the deleted item
1 vararr = ["AAA", "BBB", "CCC"];2 varUnshiftres = Arr.unshift ("ddd", "Eee");3Console.log (Unshiftres);//5 (returns the length of the new array)4Console.log (arr);//["ddd", "Eee", "AAA", "BBB", "CCC"]5 varshiftres=Arr.shift ();6Console.log (Shiftres);//DDD7Console.log (arr);//["Eee", "AAA", "BBB", "CCC"]
Slice (intercept start position, intercept end position) Slice can have two parameters, the first parameter is required, the second parameter is optional, when there is only one parameter, the default intercept to the end of the array, and the second parameter can have positive and negative two cases, If the second argument is a negative number, the ending position of the intercept is counted from the end.
1 vararr = [1,2,3,4,5];2 vararr1 = arr. Slice (1);3 varARR2 = arr. Slice (1,3);4 varARR3 = arr. Slice (1,-1);5 varARR4 = arr. Slice ( -3,-1);6Console.log (ARR1);//[2,3,4,5]7Console.log (ARR2);//[2,3]8Console.log (ARR3);//[2,3,4]9Console.log (ARR4);//[3,4]
- Array Universal method Splice ()
Splice (): Very powerful array method, it has many kinds of usages, can implement delete, insert and replace.
Delete: You can delete any number of items by specifying only 2 parameters: the location of the first item to delete and the number of items to delete. For example, splice (0,2) deletes the first two items in the array.
Insert: You can insert any number of items to a specified location, providing only 3 parameters: Start position, 0 (number of items to delete), and items to insert. For example, splice (2,0,4,6) inserts 4 and 6 starting at position 2 of the current array.
Replace: You can insert any number of items at the specified location and delete any number of items at the same time by specifying 3 parameters: The starting position, the number of items to delete, and any number of items to insert. The number of items inserted does not have to be equal to the number of items deleted. For example, splice (2,1,4,6) deletes the entry for the current array position 2, and then inserts 4 and 6 from position 2.
The splice () method always returns an array containing the items removed from the original array, and an empty array if no items are deleted.
1 vararr = [1,3,5,7,9,11];2 vararrremoved = Arr.splice (0,2);3Console.log (arr);//[5,7, 9, one]4Console.log (arrremoved);//[1,3]5 varArrremoved2= Arr.splice (2,0,4,6);6Console.log (arr);//[5, 7, 4, 6, 9, one]7Console.log (ARRREMOVED2);//[]8 varArrRemoved3 = Arr.splice (1,1,2,4);9Console.log (arr);//[5, 2, 4, 4, 6, 9, one]TenConsole.log (ARRREMOVED3);//[7]
- Array traversal foreach ()
1 vararr = [1,2,3,4,5]2Arr.foreach (function(item,index) {3 Console.log (Index,item)4 })5 //the output is:6 //0 17 //1 28 //2 39 //3 4Ten //4 5
1 var arr = [1,2,3,4,5]; 2 var arr2 = Arr.map (function(item) {3 return item+10; 4 }); 5 // [11,12,13,14,15]
The commonly used array method in JS