Creation and manipulation of arrays

Source: Internet
Author: User

Array Arrays

methods for creating methods

1.var arr = new Array ();

Arr[0] = 1;

ARR[1] = 2;

ARR[2] = 3;

Create an array with a content of
2.var arr = new Array (5);

Create an array of length 5

3.var arr = new Array (1, 2, 3);

Create an array with a content of

4.var arr = [1, 2, 3];

Operation of the array

1. Array name. Shift ();

Deletes the first element and returns the value

var arr = [1, 2, 3];

var a = Arr.shift ();

alert (a);

Alert (arr);

2. Array name. Pop ()

Deletes the last element and returns the value

var arr = [1, 2, 3];

var a = Arr.pop ();

alert (a);

Alert (arr);

3. Array name. unshift (insert content);

Insert data to the front and return its length

var arr = [1, 2, 3];

var a = Arr.unshift (4,5);

alert (a);

Alert (arr);

4. Array name. push (insert content);

Insert data to the last face and return its length

var arr = [1, 2, 3];

var a = Arr.push (4,5);

alert (a);

Alert (arr);

5. Array name. Splice (start index, number of deletions, data to be inserted);

var arr=[1,2,3];

var a = Arr.splice (1, 0, 4, 5,7);

alert (a);

Alert (arr);

alert (arr.length);

6. Reverse Order

Array name. reverse ();

var arr=[1,2,3];

var reverse = Arr.reverse ();

Alert (arr);

alert (reverse);

7. Array name. concat (another array): concatenation of two arrays

var arr=[1,2,3];

var a=[4,5,6];

var b=arr.concat (a);

alert (b);

8. Array name. Slice (start,end)

Returns a new array that consists of the entries from the original array that specify the starting subscript to the end subscript

Does not modify the array, it returns a sub-array. If you want to delete an element from an array, you should use the method Array.splice

()。

If there is no end, then the slice () method selects all elements from start to the end of the array.

var arr=[1,2,3,4,5];

var b=arr.slice (1,4);

alert (b);

Creation and manipulation of arrays

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.