Common methods of js array

There are several methods of array:

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)

Common array methods

1.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 a string in parentheses ("quotation marks required"),

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.push() and pop() methods

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(""), write content in parentheses ("string should be quoted"),

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.shift and unshift() are the opposite of the push() and pop() methods above. They are 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(""), write the content in parentheses ("the string should be quoted")

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.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.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.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. I didn't give it
When the concat() method passes parameters, it just copies the current array and returns a copy.

Writing format: arr.concat(), write the content in parentheses ("the string should be quoted"),

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)

6.slice()

Returns a new array 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. When there is 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).

7.splice()

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). 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)

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]

8.forEach()
forEach(): loop through the array and run the given function for each item in the array. This method has no return value. The parameters are all of function type. By default, there are passed parameters. The parameters are: the contents of the traversed array; The corresponding array index, the array itself.
Writing format: arr.forEach()

var arr = [1, 2, 3, 4, 5];
arr.forEach(function(x, index, a){
console.log(x + '|' + index + '|' + (a === arr));
});
// Output is:
// 1|0|true
// 2|1|true
// 3|2|true
// 4|3|true
// 5|4|true

Keywords: Javascript Front-end

Added by developer on Fri, 21 Jan 2022 19:42:16 +0200