Common array manipulation functions and their usage in JavaScript
I wrote a post yesterday summarizing the string manipulation functions and usage in the common JavaScript. Today, there is time, but also to the common JavaScript array operation function and usage summary, so convenient for everyone to prepare reference. If you are also preparing a variety of written tests, I hope to help you. It is also welcome to add.
1. Array creation
Creating an array should be the simplest, and there are two methods for creating and grouping constructors of array literals, as shown below:
var array1 = new Array (); var array2 = [];
These are the two most common ways to create an array, and the second method is highly recommended by developers because it is simple and intuitive. Where you create an array using a constructor, you can also receive values for storing in an array, such as:
var array3 = new Array ("Num1", "num2", "num3"),//["Num1", "num2", "num3"]
Similarly, if the value you pass to the constructor is a number, then if it is greater than 0, it will be the length of the newly created array, such as:
var array4 = new Array (5); ARRAY4.LENGTH;//5
With the same effect, we recommend creating arrays in the form of literals.
2. Array detection
There are two ways to detect arrays, the first of which is to use instanceof, as follows:
var array5 = [];array5 instanceof array;//true
At the same time, the array also comes with the IsArray () method, as follows:
var array6 = []; Array.isarray (ARRAY6);//true
3. Get the array length
The length of the array can be obtained by. length, as follows:
var array7 = [1,2,3,4];ARRAY7.LENGTH;//4
At the same time, the length of the array you can also be set at any time, if the length of the set is longer than before, then the content will automatically supplement the undefined, otherwise it will intercept the effective length of the content, as follows:
var array8 = [1,2,3,4,5];console.log (array8.length);//5array8.length = 8;array8;//[1, 2, 3, 4, 5, Undefinedx3]array8.len Gth = 3;array8;//[1,2,3]
4. Gets or sets the array value
We can get and set the value of the array, which we need to remember is that the subscript of the array starts with 0, as follows:
var array9 = [1,2,3,4,5,6];array9[2];//3array9[2] = 4;ARRAY9[2];//4
5. Array string Conversion
The array is converted to a string that can call the array's own ToString () method, returning the string form of the array as follows:
var array10 = [1,2,3,4];array10.tostring ();//"1,2,3,4"
In addition, the array has another very useful function join (), which accepts a string parameter that is used to interpolate between the array items to form a string, as follows:
var array11 = [1,2,3,4];array11.join ("| |"); /"1| | 2| | 3| | 4 "
6. Array additions and deletions
As we said earlier, we can set the length to automatically add some undefined values to the array. At the same time, we can add items to an array by accessing a numeric subscript that exceeds the length of the array, such as:
var array12 = [1,2,3,4];array12[5] = 5;array12[6] = 6;array12;//[1, 2, 3, 4, UNDEFINEDX1, 5, 6]
Alternatively, we can add a new element to the array via push (), as follows:
var array13 = [1,2,3,4];array13.push (5,6); array13;//[1, 2, 3, 4, 5, 6]
In contrast to push () there is a pop () method that deletes an item from an array and starts with the last item in the array, for example:
var array14 = [1,2,3,4];array14.pop ();//4array14.pop ();//3;array14;//[1, 2]
At the same time, we can delete an array item by deleting it, but only delete the value and restore the default undefined, as follows:
var array15 = [1,2,3,4,5];d elete array15[1];array15;//[1, Undefinedx1, 3, 4, 5]
Two other very useful methods, shift () and unshift (), are similar to the push () and Pop () methods, where the shift () method is used to remove elements from the beginning of the array and return the deleted elements, as follows:
var array16 = [1,2,4,5,6];array16.shift ();//1array16; [2,4,5,6]
Unshift (), instead of shift (), is used to add elements to the starting position of the array, and the original elements of the array move back, as follows:
var array17 = [1,2,3,4];array17.unshift (2);//returns array length 5, same as Array17.unshift (3); Array17.unshift (4); array17;//[4, 3, 2, 1, 2 , 3, 4]
7. Array flipping and sorting
We can flip an array or let an array be sorted, as follows:
var array18 = [21,14,54,35,23,44,103];array18.reverse ();//[103, 14, 21, 23,//[103, 21]array18.sort (); 35, 44, 54]
Note that the default ordering of arrays is not size, but rather the corresponding strings are sorted by encoding. You can pass a comparison function to sort (), and the class changes the rule as follows:
var array18 = [21,14,54,35,23,44,103];array18.reverse ();//[103, N, A, (a), 21]array18.sort (A, b) { return a-B;}); /[14, 21, 23, 35, 44, 54, 103]
8. Array connection
We can concatenate different arrays together, as follows:
var array19 = [1,2,3];var array20 = [4,5];var Array21 = Array19.concat (array20);//[1, 2, 3, 4, 5]
Note that the original array is not and will not change.
9. Array segmentation
We can also divide the array by slice (), which takes a parameter to indicate the starting position of the split, and accepts the second optional argument as the end position, as follows:
var array22 = [1,2,3,4,5,6];var array23 = Array22.slice (4);//5,6var array24 = Array22.slice (2,4);//3,4
Note that the segmented array contains the starting position of the slice () and does not contain the end position.
10, the most powerful splice ()
Now, it's time to talk about the most powerful splice () function, which accepts three parameters that can be added, deleted, modified, and so on. Look at the syntax of the w3school on it, as follows:
① adding elements
var array25 = [1,2,3,4,5,6];array25.splice (2,0,88,77);//[] returns the deleted element, not deleted here, returned as empty array25;//[1, 2, 88, 77, 3, 4, 5, 6]
② modifying elements
var array26 = [1,2,3,4,5,6,7];array26.splice (2,2,33,44);//[3,4]array26;//[1, 2, 33, 44, 5, 6, 7]
③ deleting elements
var array27 = [1,2,3,4,5,6,7];array27.splice (2,2);//[3, 4]array27;//[1, 2, 5, 6, 7]
Conclusion
Hastily summed up the next, do not know if there are any key functions or methods, I hope you can give a supplement!
Common array manipulation functions and their usage in JavaScript