How to add and delete array elements in JS array: code example _ javascript skills

Source: Internet
Author: User
This article mainly introduces the code example of adding and deleting methods to and from JS array elements. This article provides an example of operating code directly, if you want to add or delete the js array elements, you will be confused. today, I finally find the detailed information and give me the code to test. ^-^

Var arr = new Array (); arr [0] = "aaa"; arr [1] = "bbb"; arr [2] = "ccc "; // alert (arr. length); // 3arr. pop (); // alert (arr. length); // 2 // alert (arr [arr. length-1]); // bbbarr. pop (); // alert (arr [arr. length-1]); // aaa // alert (arr. length); // 1var arr2 = new Array (); // alert (arr2.length); // 0arr2 [0] = "aaa"; arr2 [1] = "bbb "; // alert (arr2.length); // 2arr2. pop (); // alert (arr2.length); // 1arr2 = arr2.slice (0, arr2.length-1); // alert (Arr2.length); // 0arr2 [0] = "aaa"; arr2 [1] = "bbb"; arr2 [2] = "ccc"; arr2 = arr2.slice (0, 1 ); alert (arr2.length); // 1 alert (arr2 [0]); // aaaalert (arr2 [1]); // undefinedshift: delete the first entry of the original array, return the value of the deleted element. If the array is empty, return undefined var a = [, 5]; var B =. shift (); // a: [2, 3, 4, 5] B: 1 unshift: add the parameter to the beginning of the original array, and return the array length var a = [1, 2, 3, 4, 5]; var B =. unshift (-2,-1); // a: [-2,-,] B: 7 Note: in IE6.0, the returned value is always undefined, the test return value in FF2.0 is 7. Therefore, the return value of this method is not reliable and must be replaced by splice when the return value is used. Pop: delete the last entry of the original array and return the value of the deleted element. If the array is empty, return undefined var a = [1, 2, 3, 4, 5]; var B =. pop (); // a: [,] B: 5 // you can directly call this operation without returning it: add the parameter to the end of the original array, returns the array length var a = [1, 2, 3, 4, 5]; var B =. push (6, 7); // a: [1, 3, 4, 5, 6, 7] B: 7 concat: returns a new array, is to add parameters to the original array to form var a = [1, 2, 3, 4, 5]; var B =. concat (6, 7); // a: [1, 3, 4, 5] B: [1, 2, 3, 4, 5, 7] splice (start, deleteCount, val1, val2 ,...): delete the deleteCount item from the start position, and insert val1, val2 ,... Var a = [1, 2, 3, 4, 5]; var B =. splice (, 9); // a: [,] B: [] var B =. splice (0, 1); // same as shift. splice (0, 0,-2,-1); var B =. length; // same as unshift var B =. splice (. length-1, 1); // same as pop. splice (. length, 0, 6, 7); var B =. length; // same as push reverse: returns the reverse order of the array var a = [1, 2, 3, 4, 5]; var B =. reverse (); // a: [5, 4, 3, 2, 1] B: [5, 4, 3, 2, 1] sort (orderfunction ): sort the array by the specified parameter var a = [1, 2, 3, 4, 5]; var B = A. sort (); // a: [1, 2, 3, 4, 5] B: [1, 2, 3, 4, 5] slice (start, end ): returns the array var a = [1, 2, 4, 5]; var B =. slice (); // a: [, 5] B: [, 5] join (separator): groups the elements of the array into a string, use separator as the separator, otherwise use the default comma as the separator var a = [1, 2, 4, 5]; var B =. join ("|"); // a: [1, 2, 3, 4, 5] B: "1 | 2 | 3 | 4 | 5": use arrays to simulate javaStringBuffer to process strings:/*** string processing function */function StringBuffer () {var arr = new Array; this. append = functio N (str) {arr [arr. length] = str ;}; this. toString = function () {return arr. join (""); // ping the append array into a string};} today, in the application, we suddenly found that join is a good way to convert the array into a string, therefore, it is encapsulated into an object./*** is used to convert the array into a string separated by a specific symbol */function arrayToString (arr, separator) {if (! Separator) separator = ""; // If separator is null, return arr is empty by default. join (separator);}/*** find the string contained in the array */function arrayFindString (arr, string) {var str = arr. join (""); return str. indexOf (string );}

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.