Some of the functions of the array operation in JavaScript

Source: Internet
Author: User

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

Note that if a new value is given directly to the assignment, the Array length属性 change will result Array长度 :

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 = [1, 2, 3];arr[1] =//  arr now changes to [1, ' HH ', 3]

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.

IndexOf

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

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

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

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 (//  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 copy one Array :

var arr = [' A ', ' B ', ' C ', ' D ', ' E ', ' F ', ' G ']; var acopy =//  [' A ', ' B ', ' C ', ' D ', ' E ', ' F ', ' G ']//  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 (3//  return array new length: 4//  [1, 2, 3, 4]//< /c6> pop () return 4//  [1, 2, 3]//  continuous pop 3 times //  []   empty array continuation pop does not error, but returns undefined//  []
Unshift and Shift

If you want to add several elements to the head of the Array , use the unshift () method, and the shift () method adds the Array Delete the first element:

var arr = [1, 2];arr.unshift (//  return array new length: 4//  [' A ', ' B ', 1, 2] c6>//  ' A '//  [' B ', 1, 2]//  continuous shift 3 times //  [] // empty array Continue shift does not error, but returns undefined //  []
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 '//  [' A ', ' B ', ' C ']

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

Reverse

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

var arr = [' One ', ' one ', ' three'//  [' 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:

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 ']
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//  [' 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]
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];var New_arr = 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.

Some of the functions of the array operation in JavaScript

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.