JavaScript Basics Collection (ii)

Source: Internet
Author: User

This series is mainly for the basic knowledge of JS related to make a summary, but also most novice easy to confuse the place.

(1) Slice

Slice () is the substring () version of the corresponding string, which intercepts some elements of the array and returns a new array:

var arr = [' A ', ' B ', ' C ', ' D ', ' E ', ' F ', ' G '];
Arr.slice (0, 3); Starting at index 0, ending with index 3, but excluding index 3: [' A ', ' B ', ' C ']
Arr.slice (3); Starting from index 3 to end: [' D ', ' E ', ' F ', ' G ']
   Note that the start and end parameters of slice () include the starting index, not including the ending index.
If you do not pass any arguments to slice (), it will intercept all elements from beginning to end. With this, we can easily copy an array:
var arr = [' A ', ' B ', ' C ', ' D ', ' E ', ' F ', ' G '];
var acopy = Arr.slice ();
Acopy; [' A ', ' B ', ' C ', ' D ', ' E ', ' F ', ' G ']
Acopy = = = Arr; False

(2) Push and pop

Push () adds several elements to the end of the array, and pop () removes the last element of the array:
var arr = [1, 2];
Arr.push (' A ', ' B '); Returns the new length of array: 4
Arr [1, 2, ' A ', ' B ']
Arr.pop (); pop () delete ' B ', and return ' B '
Arr [1, 2, ' A ']
Arr.pop (); Arr.pop (); Arr.pop (); Consecutive POPs 3 times
Arr // []
Arr.pop (); empty array continuation pop does not error, but returns undefined
Arr // []

(3) Unshift and shift

If you want to add several elements to the head of an array, using the Unshift () method, the Shift () method deletes the first element of the array:
var arr = [1, 2];
Arr.unshift (' A ', ' B '); Returns the new length of array: 4
Arr [' A ', ' B ', 1, 2]
Arr.shift (); A
Arr [' B ', 1, 2]
Arr.shift (); Arr.shift (); Arr.shift (); Continuous Shift 3 times
Arr // []
Arr.shift (); empty array Continue shift does not error, but returns undefined
Arr // []

(4) Splice

The splice () method modifies the "universal method" of an array, which deletes several elements from the specified index, and then adds several elements from that location:

var arr = [' Microsoft ', ' Apple ', ' Yahoo ', ' AOL ', ' Excite ', ' Oracle '];
Delete the 3 elements starting at index 2, and then add the two elements:
Arr.splice (2, 3, ' Google ', ' Facebook '); return deleted elements [' Yahoo ', ' AOL ', ' Excite ']
Arr [' Microsoft ', ' Apple ', ' Google ', ' Facebook ', ' Oracle ']
Delete only, do not add:
Arr.splice (2, 2); [' Google ', ' Facebook ']
Arr [' Microsoft ', ' Apple ', ' Oracle ']
Add only, do not delete:
Arr.splice (2, 0, ' Google ', ' Facebook '); return [] because no elements were removed
Arr [' Microsoft ', ' Apple ', ' Google ', ' Facebook ', ' Oracle ']

(5) Concat 

The Concat () method joins the current array with another array and returns a new array:
var arr = [' A ', ' B ', ' C '];
var added = Arr.concat ([1, 2, 3]);
Added [' A ', ' B ', ' C ', 1, 2, 3]
Arr [' A ', ' B ', ' C ']
Note that the concat () method does not modify the current array, but instead returns a new array.

In fact, the concat () method can receive any element and array, and automatically takes the array apart and adds it all to the new array:

var arr = [' A ', ' B ', ' C '];
Arr.concat (1, 2, [3, 4]); [' A ', ' B ', ' C ', 1, 2, 3, 4]

(6) Join    

The join () method is a useful way to concatenate each element of the current array with a specified string and then return the concatenated string:
var arr = [' A ', ' B ', ' C ', 1, 2, 3];
Arr.join ('-'); ' A-b-c-1-2-3 '
If the element of the array is not a string, it is automatically converted to a string and then concatenated.

JavaScript Basics Collection (ii)

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.