Operations on Javascript Arrays

Source: Internet
Author: User

Operations on Javascript Arrays
Unshift and shift

Var arr = [1, 2, 3]; arr. unshift (4); // arr = [4, 1, 2, 3]; add arr to the header. shift (); // arr = [1, 2, 3]; Delete the header

Tail Operation push and pop

Var arr = [1, 2, 3]; arr. push (4); // arr = [1, 2, 3, 4]; add arr at the end. pop (); // arr = [1, 2, 3]; tail deletion

Universal splice operation

Var arr = [1, 2, 3, 4]; arr. splice (1, 0, "a", "B"); // arr = [1, 2, "a", "B", 3, 4]; add the arr element. splice (1, 2); // arr = [1, 2, 3, 4]; deletes the element arr. splice (1, 2, "a", "B"); // arr = [1, 2, "a", "B"]; replace Element

Array connection concat

var arr1 = [1, 2];var arr2 = [3, 4];var arr3 = arr1.concat(arr2); // arr3 = [1, 2, 3, 4]

Array conversion string join

var arr = [1, 2, 3, 4];var s = arr.join("-"); // a = "1-2-3-4";

Array sorting sort

// Positive Sorting of the number array. sort (function (a, B) {if (a> B) {return 1;} else if (a <B) {return-1 ;} else {return 0 ;}}); // Optimized Code. sort (function (a, B) {return a-B ;});

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.