Common array operation functions and usage in JavaScript

Source: Internet
Author: User

Common array operation functions and usage in JavaScript
I wrote a post yesterday to summarize the common string operation functions and usage in JavaScript. Today, we have time to summarize the common array operation functions and usage in JavaScript for your reference. If you are preparing a variety of tests, I hope it will help you. At the same time, you are also welcome to add. 1. Creating arrays should be the easiest way to create arrays. You can use two methods: Array literal creation and Array constructor. For details, see var array1 = new Array (); var array2 = []; the preceding two methods are most common for creating arrays. The second method is favored by developers because it is simple and intuitive. The constructor can also be used to create an Array to receive values stored in the Array, for example, var array3 = new Array ("num1", "num2", "num3 "); // ["num1", "num2", "num3"] Similarly, if the value you pass to the constructor is a number, if the value of this number is greater than 0, the length of the newly created Array, for example, var array4 = new Array (5); array4.length; // 5, we recommend that you use a literal to create an array. 2. There are two methods for Array Detection: the first method uses instanceof, as shown below: var array5 = []; array5 instanceof Array; // true at the same time, array also comes with the isArray () method, as follows: var array6 = []; Array. isArray (array6); // true 3. You can use. get the length as follows: var array7 = [1, 3, 4]; array7.length; // 4 at the same time, you can set the length of the array at any time. If the length of the array exceeds the previous value, the following content is automatically added to the dimension undefined; otherwise, the valid length is truncated, as follows: var array8 = [1, 2, 3, 4, 5]; console. log (array8.length); // 5array8. length = 8; array8; // [1, 2, 3, 4, 5, Undefined × 3] array8.length = 3; array8; // [, 3] 4. Obtain or set the array value. We can obtain and set the array value, note that the subscript of the array starts from 0, as shown below: var array9 = [1, 2, 4, 5, 6]; array9 [2]; // 3array9 [2] = 4; array9 [2]; // 4 5. to convert an array to a string, you can call the toString () method that comes with the array, returns the string format of the array, as follows: var array10 = [1, 2, 3, 4]; array10.toString (); // "1, 2, 3, 4". In addition, an array also has another very useful function, join (), which accepts a string parameter to form a string inserted between the array items, as shown below: var array11 = [1, 2, 4]; array11.join ("|"); // "1 | 2 | 3 | 4" 6. Add an array and As mentioned above, we can set the length to automatically add undefined values to the array. At the same time, we can also access a numeric subscript that exceeds the original length of the array to add items to the array, such as: var array12 = [1, 2, 3, 4]; array12 [5] = 5; array12 [6] = 6; array12; // [1, 2, 3, 4, undefined × 1, 5, 6]. In addition, we can also push () add new elements to the array, as shown in the following code: var array13 = [1, 2, 3]; array13.push (5, 6); array13; // [1, 2, 3, 4, 5, 6] There is a pop () method opposite to push (), which is used to delete the items in the array and starts from the last item of the array, for example: var array14 = [,]; array14.pop (); // 4array14. pop (); // 3; array14; // [1, 2] At the same time, we can delete an array item through delete, but only this value will be deleted to restore the default unde Fined, as follows: var array15 = [1, 2, 3, 4, 5]; delete array15 [1]; array15; // [1, undefined × 1, 3, 4, 5] the other two very useful methods shift () and unshift () are similar to the push () and pop () methods, where shift () the method is used to delete an element from the starting position of the array and return the deleted element, as follows: var array16 = [, 6]; array16.shift (); // 1array16, 5, 6] While unshift () is opposite to shift (), it is used to add elements to the starting position of the array, and the original elements of the array are moved backward, as follows: var array17 = [1, 2, 3, 4]; array17.unshift (2); // The returned array length is 5, the same as array17.unshift (3); array17.unshift (4); array17 ;//[ 4, 3, 2, 1, 2, 3, 4] 7. You can flip an array or sort an array as follows: var array18 = [103, 54,35, 103,]; array18.reverse (); // [, 44, 23, 35, 54, 14, 21] array18.sort (); // [103, 14, 21, 23, 35, 44, 54] note that the default sorting of arrays is not the size, but the encoding of corresponding strings. You can pass a comparison function to sort () to change the class rule, as follows: var array18 = [103, 14, 54,35, 103,]; array18.reverse, 44, 23, 35, 54, 14, 21] array18.sort (function (a, B) {return a-B;}); // [14, 21, 23, 35, 44, 54,103] 8. array connection we can connect different arrays, as shown in the following code: var array19 = [, 3]; var array20 = []; var array21 = array19.concat (array20); // [1, 2, 3, 4, 5] note that the original array does not change. 9. array split we can also split the array through slice (). If it accepts a parameter, it indicates the start position of the split and the second optional parameter indicates the end position, var array22 = [1, 3, 4, 5, 6]; var array23 = array22.slice (4); // 5, 6var array24 = array22.slice (2, 4); // 3, 4 note, the split array contains the starting position of slice (), not the ending position. 10. The most powerful splice (). Now, let's talk about the most powerful splice () function. It accepts three parameters to implement different functions, such as adding, deleting, and modifying. Take a look at its syntax on w3school, as shown below: ① Add the element var array25 = [,]; array25.splice ); // [] returns the deleted element, which is not deleted. The returned result is an empty array25; // [1, 2, 88, 77, 3, 4, 5, 6] ② modify the element var array26 = [1, 3, 4, 5, 6, 7]; array26.splice (2, 2, 3, 44); // [3, 4] array26; // [1, 2, 33, 44, 5, 6, 7] ③ Delete the element var array27 = [1, 2, 4, 5, 6, 7]; array27.splice (2, 2); // [3, 4] array27; // [1, 2, 5, 6, 7] Conclusion

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.