Fully understand the meaning of arrays: the use of various array methods __ Array

Source: Internet
Author: User
Tags array length


1. Instrumentation array: Array.isarray (value) support Ie9+,ff4,safari5+,opera 10.5+,chrome
2. Conversion method: ToString (), valueof ()
3. Stack method: LIFO, Push () add the parameter to the end of the array, the POP () method removes the last item from the end of the array and returns the item
4. Queue method: Advanced first out, push () add the parameter to the end of the array, shift () move the first item in the divisor group and return the item
5. Reorder methods:
Reverse () Reverse Array Item order
Sort () By default the array items are sorted in ascending order, the ToString () method is invoked automatically, and a comparison function, compare (value1,value2), can be accepted as an argument, and the comparison function receives two parameters:

function Compare1 (value1,value2) {return
           value1-value2;   Ascending
}
function Compare2 (value1,value2) {return
           value2-value1;   Descending
}
var values=[0,104,20,46,2,5]
Values.sort (compare1);
Values.sort (Compare2);

6. Method of operation
(1) concat () creates a new array based on all the items in the current array, adding items to the end of the array without affecting the original array:

var colors=["Red", "green", "Blue"];
var colors2=color.contact ("Pink", "black");

var myarr1= new Array ("010")
var myarr2= new Array ("-", "84697581");  
document.write (Myarr1.concat (MYARR2));

(2) Slice () creates a new array based on one or more items in the current array, accepting one or two arguments, that is, to return the starting and ending positions of the items:

var color3=colors.slice (1,2);

(3) Splice () method: Inserts an item into the middle of the array, important ...
Delete: Splice (index,num) The position of the first item to delete and the number of items to delete
Insert: Splice (index,0,) Start position, number of items to delete 0 and items to insert
Replace: Splice (index,num,) Start position, number of items to delete, and replaced items
The splice () method always returns an array containing the items removed from the original array, or an empty array if none is returned
(4) The Join () method is used to put all elements in an array into a string. The elements are delimited by the specified delimiter. Arrayobject.join (separator)

7. Location method
IndexOf () begins a backward lookup from a location in the array to return the first occurrence of a specified string value in the string:
Syntax: stringobject.indexof (substring, startpos) Specify the retrieved string value, the position in the string to begin retrieving (default start from scratch)

var mystr= "Hello world!"; /retrieves the position where the second character o appears.
document.write (mystr.indexof (' O ', mystr.indexof (' O ') +1));

LastIndexOf () looks forward from the end of the array.
Support Browser: Ie9+,ff2+,safari3+,opera 9.5+,chrome
8. Iterative method
9. Merge method

Instance:

 <! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> 

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.