Javascript Array Using call method aggregation _ javascript tips

Source: Internet
Author: User
Tags javascript array
Javascript array summary JS array method summary using call Methods
Shift: Delete the first entry of the original array and return the value of the deleted element. If the array is empty, undefined is returned.
Var a = [1, 2, 3, 4, 5];
Var B = a. shift (); // a: [2, 3, 4, 5] B: 1

Unshift: add the parameter to the beginning of the original array and return the length of the array.
Var a = [1, 2, 3, 4, 5];
Var B = a. unshift (-2,-1); // a: [-2,-,] B: 7
Note: In IE6.0, the test return value is always undefined, and in FF2.0, the test return value is 7. Therefore, the return value of this method is unreliable. You need to use splice instead of this method when returning the value.

Pop: Delete the last entry of the original array and return the value of the deleted element. If the array is empty, undefined is returned.
Var a = [1, 2, 3, 4, 5];
Var B = a. pop (); // a: [1, 2, 3, 4] B: 5

Push: add the parameter to the end of the original array and return the length of the array.
Var a = [1, 2, 3, 4, 5];
Var B = a. push (6, 7); // a: [1, 2, 3, 4, 5, 6, 7] B: 7

Concat: returns a new array consisting of adding parameters to the original array.
Var a = [1, 2, 3, 4, 5];
Var B = a. concat (6, 7); // a: [1, 2, 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 = a. splice (, 9); // a: [,] B: []
Var B = a. splice (0, 1); // same as shift
A. splice (0, 0,-2,-1); var B = a. length; // same as unshift
Var B = a. splice (a. length-1, 1); // same as pop
A. splice (a. length, 7); var B = a. length; // same as push

Reverse: returns the reverse order of the array.
Var a = [1, 2, 3, 4, 5];
Var B = a. reverse (); // a: [5, 4, 3, 2, 1] B: [5, 4, 3, 2, 1]

Sort (orderfunction): sorts arrays by specified parameters.
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 a new array consisting of items from the original array that specify the start subscript to the end subscript
Var a = [1, 2, 3, 4, 5];
Var B = a. slice (); // a: [, 5] B: [, 5]

Join (separator): A string is set up for the elements of the array. The separator is separator. If it is omitted, a comma is used as the separator by default.
Var a = [1, 2, 3, 4, 5];
Var B = a. join ("|"); // a: [1, 2, 3, 4, 5] B: "1 | 2 | 3 | 4 | 5"
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.