Summary of common js Array Operations

Source: Internet
Author: User

// Array division in javascript var colors = ["red", "green", "blue"]; // alert (colors. toString (); alert (colors. join ("|"); // The returned result is red | green | bluevar colors = ["red", "green", "blue", null]; alert (colors. join ("| ")); // red | green | blue | // note that when the value in the array is null or undefined, the returned result is expressed as an empty string ----------------------------------------- // Delete the array and add var colors = ["red ", "green", "blue"]; // alert (colors. toString (); colors. push ("white", "test"); // The returned result is the array length alert (colors. join ("|"); // The result is red | green | blue | white | test // Add the element var colors = ["red ", "green", "blue", "test"]; var item = colors. unshift ("first"); // Add an element alert (colors. join ("|"); // return the final array // Delete the element var colors = ["red", "green", "blue", "test"]; var item = colors. pop (); // return the delete option result testalert (colors. join ("|"); // returns the final array result red | green | blue // deletes the starting element var colors = ["red", "green ", "blue", "test"]; var item = colors. shift (); // Delete the first alert (colors. join ("|"); // return the final array ----------------------------------------------- // array sequence example // Reverse Order var colors = ["red", "green", "blue ", "test"]; colors. reverse (); alert (colors); // The result is: test, blue, green, red // array sorting var values = [, 7]; values. sort (compare); alert (values); // document. writeln (values);} function compare (value1, value2) {if (value1 <value2) {return 1;} else if (value1> value2) {return-1 ;} else return 0;} --------------------------------------------------- // Add the array concat () method var colors = ["color", "red"] to the array; var colors2 = colors. concat (["ccc", "bbbb"], '20140901', ['vvccxx', ['oolll', 'lll']); alert (colors2 ); // The returned result is: color, red, ccc, bbbb, 3333, vvccxx, oolll, lll // slice () copying the elements in the array does not destroy the previous element var colors = ["color", "red", 'eee', '000000']; var colors2 = colors. slice (1); // copy alert (colors2) from 1; // The result is: red, eeee, 221111var colors = ["color", "red ", 'eeeee', '000000']; var colors2 = colors. slice (3rd); // copy from 1 to locations and end alert (colors2); // The result is red, eeee --------------------------------------------------------------------- // Delete the element var a = [1, 2, 3, 5, 8] in the array; var r =. splice (); // Delete the first two alert (a); // The result is, 8var a = [, 8]; var r =. splice (100,200, 2nd); // delete an item starting from 100 and insert 200 1,100,200 alert (a); // The result is, 8

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.