How do I work with arrays?

Source: Internet
Author: User

Pop (): deletes the last item in the array. You do not need to write parameters to return the value of the deleted item.

Push (): adds data at the end of the array. Parameter: is the data to be added, and multiple items are separated by commas. Returns the length of the array of values.

Shift (): Deletes the first item of the array. You do not need to write parameters to return the value of the deleted item.

Unshift (): adds data at the beginning of the array. Parameter: is the data to be added, and multiple items are separated by commas. Returns the length of the array of values.

1//push () is added at the end of the array

2 Console.log (Arr.push (10));

3 Console.log (arr);

1//Shift () deletes the first item of the array.

2 Console.log (Arr.shift ());

3 Console.log (arr);

1//Unshift () is added at the beginning of the array.

2 Console.log (arr.unshift (0));

3 Console.log (arr);

Case: ["Spring", "Summer", "Autumn","Winter") Move the last item of the array to the beginning

1 var arr1 = [" Spring ", " summer ", " Autumn ", " winter "];

2 Arr1.unshift (Arr1.pop ());

3 Console.log (ARR1);

L Merging and splitting of arrays

Concat (): used for merging two or more arrays. The arguments are the arrays to be merged. The return value is the new array after the merge. Does not change the original array.

1 var arr1 = [1,2,3,4,5,6];

2 var arr2 = [7,8,9];

3 var arrnew = Arr1.concat (ARR2);

4 Console.log (arrnew);

5 Console.log (ARR1);

The concat() parameter is flexible, and can be an array literal or an array variable , or it can be a hash value.

1 Console.log (Arr1.concat ([up], "H", "L"));

Split: Slice (start,end): Used to intercept a fragment in an array. start,end represents the index value of the array.

Start indicates the index value (including the start value) of the start item that intercepts the array to the end index value (not including end).

The return value is a truncated array fragment. Does not change the original array.

1 var arr3 = [2,3,4,5,6,7,8];

2 var arr4 = Arr3.slice (2,5);

3 Console.log (ARR4);

4 Console.log (ARR3);

Start and end can also write negative numbers. Indicates the countdown. The countdown starts from -1 . Still includes start not including end

1 var arr5 = Arr3.slice ( -5,-2);

You can also write only a start that intercepts the last item from start to the array.

1 var arr6 = Arr3.slice (2);

How to work with 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.