JavaScript Learning Diary 1 array

Source: Internet
Author: User

IndexOf

Similar to string, Array you can also indexOf() search for the position of a specified element by:

vararr = [Ten, -,' -','XYZ'];arr.indexof (Ten);//the index of element 10 is 0Arr.indexof ( -);//the index of element 10 is 1Arr.indexof ( -);//element 30 not foundArr.indexof (' -');//the index of element ' 30 ' is 2
Slice

slice()Is the version of the corresponding string substring() , which intercepts Array the part of the element and then returns a new one 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 ']

Notice that slice() the start and end parameters include the starting index, not the ending index.

If you don't slice() pass any arguments, it intercepts all the elements from the beginning to the end. With this, we can easily copy one 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

Push and pop

push()Arrayadd a number of elements to the end, pop() then Array delete the last element:

var arr = [1, 2];arr.push (' A ', ' B '); Returns the new length of the array: 4arr; [1, 2, ' A ', ' B ']arr.pop (); Pop () returns ' B ' arr; [1, 2, ' A ']arr.pop (); Arr.pop (); Arr.pop (); Consecutive POPs 3 times arr; []arr.pop (); The empty array continues to pop without error, but returns Undefinedarr; // []

Unshift and Shift

If you want to Array add several elements to the head, using the unshift() method, the shift() Array first element of the method is deleted:

var arr = [1, 2];arr.unshift (' A ', ' B '); Returns the new length of the array: 4arr; [' A ', ' B ', 1, 2]arr.shift (); ' A ' arr; [' B ', 1, 2]arr.shift (); Arr.shift (); Arr.shift (); Continuous shift 3 times arr; []arr.shift (); An empty array continues shift without error, but returns Undefinedarr; // []

Sort

sort()You can sort the current Array , and it will directly modify the current Array element position and, when called directly, in the default order:

var arr = [' B ', ' C ', ' A '];arr.sort (); arr; [' A ', ' B ', ' C ']

Reverse

reverse()Give the whole Array element away, that is, reverse:

var arr = [' One ', ' both ', ' three '];arr.reverse (); Arr [' Three ', ' one ', ' one ']

Splice

splice()Method is a modified Array "universal method" that removes several elements from the specified index, and then adds several elements from that location:

var arr = [' Microsoft ', ' Apple ', ' Yahoo ', ' AOL ', ' Excite ', ' Oracle '];//remove 3 elements from index 2, and then add two elements: Arr.splice (2, 3, ' Google ', ' Facebook '); Returns the deleted element [' Yahoo ', ' AOL ', ' Excite ']arr; [' Microsoft ', ' Apple ', ' Google ', ' Facebook ', ' Oracle ']//only deleted, not added: Arr.splice (2, 2); [' Google ', ' Facebook ']arr; [' Microsoft ', ' Apple ', ' Oracle ']//only add, do not delete: Arr.splice (2, 0, ' Google ', ' Facebook '); return [], because no element was removed from arr; [' Microsoft ', ' Apple ', ' Google ', ' Facebook ', ' Oracle ']

Concat

concat()Method connects the current Array and the other Array , and returns a new one 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 one Array .

In fact, the concat() method can receive any element and Array automatically Array disassemble it and add it all to the new one Array :

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

Join

join()method is a very useful method that Array connects each current element with a specified string and then returns the concatenated string:

var arr = [' A ', ' B ', ' C ', 1, 2, 3];arr.join ('-'); ' A-b-c-1-2-3 '

If Array the element is not a string, it is automatically converted to a string and then concatenated.

Multidimensional arrays

If an element of an array is another Array , a multidimensional array can be formed, for example:

var arr = [[1, 2, 3], [400, 500, 600], '-'];

Example 1:var arr = [[1, 2, 3], [400, 500, 600], '-']; How to take 500 this value by index:

var arr = [[1, 2, 3], [[n], [+], '-']; var x = arr[1][1

Example 2: At the freshman welcome party, you have got a list of new classmates, please sort after show:欢迎XXX,XXX,XXX和XXX同学!

Alert (' Welcome ' + arr.slice (0,3). Join (', ') + ' and ' +arr.splice (3,1) + ' classmate ');

JavaScript Learning Diary 1 array

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.