JavaScript Basics (5) arrays

Source: Internet
Author: User

Array

ArrayProvides a function to store a set of elements sequentially, and can be read and written by index. JavaScript Array can contain arbitrary data types, and each element is accessed through an index.

To get Array the length, directly access the length properties:

var NULL true  //  6

Please note that Array length assigning a new value directly to the given will result in a Array change in size:

var arr = [1, 2, 3//  3arr.length = 6//  arr becomes [1, 2, 3, Undefined, Undefined, undefined]arr.length = 2//  arr becomes [1, 2]

ArrayThe corresponding element can be modified by the index to a new value, so Array assigning a value to the index will modify this directly Array :

var arr = [' A ', ' B ', ' C '];arr[1] =//  arr now changes to [' A ', ' a ', ' C ']

Note that if an index is assigned a value, the index is out of range, which can also cause Array a change in size:

var arr = [1, 2, 3];arr[5] = ' x '//  arr becomes [1, 2, 3, Undefined, undefined, ' x ']

Most other programming languages do not allow direct changes to the size of the array, and an error occurs when accessing the index out of bounds. However, JavaScript Array does not have any errors. When writing code, it is not recommended to directly modify Array the size of the index to ensure that the index does not cross.

Array common methods of operation

(1) indexOf () is similar to string, and Array can also be used indexOf() to search for the location of a specified element:

var arr = [Ten, 10, ' + ', ' xyz '];arr.indexof (//  element index = 0//  element 20 index = 1 /c5>//  element 30 not found, return-1//  element ' 30 ' index is 2

Note that the numbers 30 and strings ‘30‘ are different elements.

(2) 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 (//  starting at index 0, to index 3 end, but not including index 3: [' A ', ' B ' , ' C ']//  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 replicate aArray

var arr = [' A ', ' B ', ' C ', ' D ', ' E ', ' F ', ' G ']; var acopy =//  [' A ', ' B ', ' C ', ' D ', ' E ', ' F ', ' G ']//  false

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

 var  arr = [1, 2];arr.push ( ' A ', ' B '); Span style= "COLOR: #008000" >//  Returns the new length of the array: 4  arr; //  [1, 2, ' A ', ' B ']  arr.pop (); //  pop () returns ' B '  arr; //  [1, 2, ' A ']  arr.pop (); Arr.pop (); Arr.pop (); //  continuous pop 3 times  arr; //  []  arr.pop (); //  empty array continuation pop does not error, but returns undefined  arr; //  []  

(4) Use   unshift () method to add several elements to the head of array , shift () method to array Delete the first element of the

 var  arr = [1, 2];arr.unshift ( ' A ', ' B ') ; //  Returns the new length of the 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; //  []  

(5) sort() You can sort the current Array , it will directly modify Array the current element position, directly call, in the default order:

var arr = [' B ', ' C ', ' a '//  [' A ', ' B ', ' C ']

Can we sort them in the order we specify? Absolutely, we'll talk about it in the function that follows.

(6) The reverse() whole Array element is given away, that is, reversal:

var arr = [' One ', ' one ', ' three'//  [' Three ', ' one ', ' one ']

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

vararr = [' 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 removedArr//[' Microsoft ', ' Apple ', ' Google ', ' Facebook ', ' Oracle ']

(8) The 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//  [' A ', ' B ', ' C ', 1, 2, 3]//  [' 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 (//  [' A ', ' B ', ' C ', 1, 2, 3, 4]

(9) The join() method is a very useful method, which Array connects each current element with the 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], '-'];

ArrayThe above consists of 3 elements, of which the first two elements are themselves Array .

JavaScript Basics (5) arrays

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.