This article mainly summarizes the array methods of javascript in detail, including the most commonly used array methods and extension methods. if you are interested, please refer to them.
I. common methods of arrays
1: join ();
Convert the array into a string for display. If you do not enter a parameter, a comma is used by default. if you enter a parameter, a parameter is used.
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];
The original array remains unchanged.
2: reverse ();
Sorts the array in reverse order, and the original array is modified.
var arr=[1,2,3];var arr2=arr.reverse();console.log(arr2); // [3,2,1];console.log(arr); // [3,2,1];
3: sort ();
By default, Array items are arranged in ascending order. the toString () method of each array item is called, and the obtained string is compared from the first place of the string.
var arr=[2,12,14,21,5];console.log(arr.sort()); //[12, 14, 2, 21, 5];
You can also input a comparison function as a parameter. If the first parameter should be in the front, the comparison function returns a value smaller than 0; otherwise, the comparison function returns a value greater than 0; if the order is irrelevant, the comparison function returns 0;
var arr=[2,12,14,21,5];console.log(arr.sort(function(a,b){return a-b})); // [2,5,12,14,21]; var arr1=[2,12,14,21,5];console.log(arr1.sort(function(a,b){return b-a})); // [21,14,12,5,2];
4: concat ();
Array merge. the original array remains unchanged.
var arr=[1,2,3];console.log(arr.concat(4,5)); // [1, 2, 3, 4, 5];console.log(arr.concat([4,5],6)); // [1, 2, 3, 4, 5, 6];console.log(arr.concat([[4,5],6])); // [1, 2, 3, [4, 5],6];console.log(arr); // [1, 2, 3];
5: slice ();
Returns Some arrays, including the array items corresponding to the first parameter, but not the array items corresponding to the second parameter. If the input parameter is smaller than 0, it is counted from the back and the last one is-1. If only one parameter is input, the returned array contains all elements from the starting position to the end of the array. The original array remains unchanged.
var arr=[1,2,3,4,5];console.log(arr.slice(1,3)); // [2,3];console.log(arr.slice(1)); // [2,3,4,5];console.log(arr.slice(1,-1)); // [2,3,4];console.log(arr); // [1,2,3,4,5];
6: splice ();
Array splicing:
1). Delete-used to delete an element. There are two parameters: the first parameter (the location of the first item to be deleted) and the second parameter (number of items to be deleted );
2). Insert-insert any element to the specified position of the array. Three parameters: the first parameter (actual location), the second parameter (0), and the third parameter (inserted item );
3). replace-insert any item element to the specified position of the array, and delete any number of items, three parameters. The first parameter (start position), the second parameter (number of deleted items), and the third parameter (insert any number of items );
Splice () returns an array composed of deletion elements, or an empty array if no elements are deleted. The original array is modified.
var arr=[1,2,3,4,5,6];console.log(arr.splice(2)); // [3,4,5,6];console.log(arr); // [1,2];console.log(arr.splice(2,0,3,4,5,6)); // [];console.log(arr); // [1,2,3,4,5,6];console.log(arr.splice(2,2)); // [3,4];console.log(arr); // [1,2,5,6];
7: push () and pop () methods, unshift () and shift () methods;
Push () and pop () stack methods. The original array changes.
The push () method adds one or more elements to the end of the array and returns the new length of the array.
The pop () method deletes the last element of the array, reduces the length of the array, and returns the deleted value.
Unshift () and shift () queue methods, first-in-first-out. The original array changes.
The unshift () method adds one or more elements to the header of an array, changes the index of an existing element, and returns the new length of the array.
The shift () method deletes the first element of the array and returns it, and changes the index of the existing element.
var arr=[1,2,3];console.log(arr.push(4)); //4;console.log(arr); //[1,2,3,4];console.log(arr.pop()); //4;console.log(arr); //[1,2,3];console.log(arr.unshift(0)); //4;console.log(arr); //[0,1,2,3];console.log(arr.shift()); //0;console.log(arr); //[1,2,3];
8: forEach ();
In forEach (), the first parameter is the element in the set, the second parameter is the index in the set, and the third parameter is the set itself.
9: map ();
Map () runs the given function for each item in the array and returns an array composed of the results of each function call. the original array is not modified.
10: every ()
Run the given function for each item of the array. if the function returns true for each item, the return value is true.
11: some ()
Run the given function for each item of the array. if the function returns true for any item, the return value is true.
12: filter ()
Run the given function for each item in the array. if this function is returned, an array consisting of true items is returned.
13: reduce () and reduceRight ();
Both methods iterate all the items in the array and construct a final returned value. The reduce () method starts from the first entry of the array and traverses the result one by one to the end. ReduceRight () starts from the last entry of the array and traverses forward to the first entry. The array is not modified.
II. extension methods
1: Array deduplication
Function unique (array) {return array. filter (function (item, index) {return array. indexOf (item) = index ;})}; var arr = [,]; console. log (unique (arr); // [1, 2, 3, 4, 5]; function unique (arr) {var arr2 = [arr [0], len = arr. length; if (! Len) {return;} for (var I = 0; I
2: remove the empty element from the array.
function deleteNullInArray(array){ return array.filter(function(item){ return item!=null; }) }var arr=[1,2,null,,,5];console.log(deleteNullInArray(arr)); //[1,2,5];