Es5 common array methods

Time:2021-7-29

The following array methods are sorted

  • join()
  • Push() and pop()
  • Shift() and unshift()
  • sort()
  • reverse()
  • concat()
  • slice()
  • splice()
  • Indexof() and lastindexof() (new in Es5)
  • Foreach () (new in Es5)
  • Map () (new in Es5)
  • Filter () (newly added in Es5)
  • Every () (Es5 added)
  • Some() (new in Es5)

1|0 join()

Join is to convert an array into a string, and then specify a connecting character for it. The default is comma(   ,)

Writing format: join (“”), write string in parentheses   (” In quotation marks “),

var arr = [1,2,3];
console.log(arr.join());     // 1,2,3
console.log(arr.join("-"));   // 1-2-3
console.log(arr);         //[1, 2, 3] (original array unchanged)

 

2|0 Push() and pop()

push():   Add the contents to the end of the array and return the modified length.

Pop (): remove the last item of the array, return the removed value, and reduce the length of the array.

Writing format: arr.push (“”), the contents are written in parentheses   (” The string should be enclosed in quotation marks “),

Writing format: arr.pop ()

var arr = ["Lily","lucy","Tom"];
var count = arr.push("Jack","Sean");
console.log(count);           // 5
console.log(arr);            // ["Lily", "lucy", "Tom", "Jack", "Sean"]
var item = arr.pop();
console.log(item);            // Sean
console.log(arr);             // ["Lily", "lucy", "Tom", "Jack"]

3|0 Shift() and unshift()    ( Contrary to the above push and pop (for the first item)

Shift(): deletes the first item of the original array and returns the value of the deleted element; If the array is empty, undefined is returned.  

 

Unshift: adds a parameter to the beginning of the original array and returns the length of the array.

    Writing format: arr.shift (“”), with contents written in parentheses   (” The string should be enclosed in quotation marks “),

var arr = ["Lily","lucy","Tom"];
var count = arr.unshift("Jack","Sean");
console.log(count);               // 5
console.log(arr);                //["Jack", "Sean", "Lily", "lucy", "Tom"]
var item = arr.shift();
console.log(item);               // Jack
console.log(arr);                // ["Sean", "Lily", "lucy", "Tom"]

4|0 sort()

Sort (): sort the items in the array from small to large

Writing format: arr.sort ()

 

var arr1 = ["a", "d", "c", "b"];
console.log(arr1.sort());           // ["a", "b", "c", "d"]

The sort () method compares strings and does not sort numbers according to the size of numbers. To achieve this, you must use a sort function

function sortNumber(a,b)
{
  return a - b
}

arr = [13, 24, 51, 3]; console.log(arr.sort());           // [13, 24, 3, 51] 
console.log(arr.sort(sortNumber));     //[3, 13, 24, 51] (array changed)

5|0 reverse()

Reverse(): reverses the order of array items.

Writing format: arr.reverse ()

var arr = [13, 24, 51, 3];
console.log(arr.reverse());         //[3, 51, 24, 13]
console.log(arr);               //[3, 51, 24, 13] (original array changed)

6|0 concat()

Concat(): add parameters to the original array. This method will first create a copy of the current array, then add the received parameters to the end of the copy, and finally return the newly constructed array. Without passing arguments to the concat () method, it simply copies the current array and returns a copy.

    Writing format: arr.concat (), contents written in parentheses   (” The string should be enclosed in quotation marks “),

var arr = [1,3,5,7];
var arrCopy = arr.concat(9,[11,13]);
console.log(arrCopy);             //[1, 3, 5, 7, 9, 11, 13]
console.log(arr);               //[1, 3, 5, 7] (original array not modified)

7|0 slice()

Slice(): returns a new array consisting of items from the specified start subscript to the end subscript in the original array. The slice () method can accept one or two parameters, that is, the start and end positions of the item to be returned. In the case of only one parameter, the slice () method returns all items from the position specified by the parameter to the end of the current array. If there are two arguments, the method returns the item between the start and end positions — but not the item at the end position.

    Writing format: arr.slice (1, 3)   )

var arr = [1,3,5,7,9,11];
var arrCopy = arr.slice(1);
var arrCopy2 = arr.slice(1,4);
var arrCopy3 = arr.slice(1,-2);
var arrCopy4 = arr.slice(-4,-1);
console.log(arr);               //[1, 3, 5, 7, 9, 11] (the original array remains unchanged)
console.log(arrCopy);             //[3, 5, 7, 9, 11]
console.log(arrCopy2);            //[3, 5, 7]
console.log(arrCopy3);            //[3, 5, 7]
console.log(arrCopy4);            //[5, 7, 9]

Arrcopy only sets one parameter, that is, the starting subscript is 1, so the returned array starts from subscript 1 (including subscript 1) to the end of the array.

Arrcopy2 sets two parameters to return a subarray of starting subscripts (including 1) and ending subscripts (excluding 4).

Arrcopy3 sets two parameters. The termination subscript is a negative number. When a negative number occurs, the number at this position is replaced by the negative number plus the value of the array length (6), so it is a sub array from 1 to 4 (not included).

Both parameters in arrcopy4 are negative numbers, so they are converted into positive numbers by adding array length 6, so they are equivalent to slice (2,5).

8|0 splice()

Splice (): delete, insert, and replace.

Delete: specify 2 parameters: the position of the first item to be deleted and the number of items to be deleted.

Writing format: arr.splice (1, 3)   )

 

Insert: you can insert any number of items into the specified location. You only need to provide three parameters: starting location, 0 (number of items to be deleted) and items to be inserted.

Writing format: arr.splice(    2,0,4,6   )
Replace: you can insert any number of items into the specified location and delete any number of items at the same time. You only need to specify three parameters: starting location, 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.

    Writing format: arr.splice(    2,0,4,6   )

var arr = [1,3,5,7,9,11];
var arrRemoved = arr.splice(0,2);
console.log(arr);                //[5, 7, 9, 11]
console.log(arrRemoved);            //[1, 3]
var arrRemoved2 = arr.splice(2,0,4,6);
console.log(arr);                // [5, 7, 4, 6, 9, 11]
console.log(arrRemoved2);           // []
var arrRemoved3 = arr.splice(1,1,2,4);
console.log(arr);                // [5, 2, 4, 4, 6, 9, 11]
console.log(arrRemoved3);           //[7]

Recommended Today

VBS obtains the operating system and its version number

VBS obtains the operating system and its version number ? 1 2 3 4 5 6 7 8 9 10 11 12 ‘************************************** ‘*by r05e ‘* operating system and its version number ‘************************************** strComputer = “.” Set objWMIService = GetObject(“winmgmts:” _  & “{impersonationLevel=impersonate}!\\” & strComputer & “\root\cimv2”) Set colOperatingSystems = objWMIService.ExecQuery _  (“Select * from […]