Javascript--array Array Objects

Source: Internet
Author: User
Tags array definition

Array Object

An array object is a collection of objects that can be of different types. Each member object of an array has a "subscript" that represents its position in the array, which is zero-based

Method of array Definition:

1. Defines an empty array:

var array name = new Array ();

2. Define an array of n empty elements when specified:

var array name =new array (n);

3. When defining an array, initialize the data directly:

var array name = [< element 1>, < element 2>, < element 3>...];

We define the MyArray array and assign the values to the code as follows:

Description: defines an array myArray, inside the element is: myarray[0] = 2; MYARRAY[1] = 8; MYARRAY[2] = 6.

Array elements are used:

Array name [subscript] = value;

Note : The subscript of the array is enclosed in square brackets, starting with 0.

Array properties:

Length usage:< Array Object >.length; return: The length of the array, which is the number of elements in the array. It equals the subscript of the last element in the array plus one.

Array method:

Array Connection concat ()

The Concat () method is used to concatenate two or more arrays. This method returns a new array without changing the original array.

Grammar

Arrayobject.concat (array1,array2,..., Arrayn)

Parameter description:

Note: This method does not alter an existing array, but simply returns a copy of the concatenated array.

We create an array that will connect the parameters in Concat () to the array Myarr, with the following code:

</script>

Operation Result:

1,2,3,4,51,2,3

We created three arrays and then used Concat () to connect them together with the code as follows:

<script type= "Text/javascript" >  var mya1= new Array ("hello!")  var mya2= new Array ("I", "Love");  var mya3= new Array ("JavaScript", "!");  var mya4=mya1.concat (MYA2,MYA3); document.write (MYA4);</script>

Operation Result:

hello!,i,love,javascript,!
Specify delimiter join array element join ()

The join () method is used to put all the elements in an array into a string. The elements are delimited by the specified delimiter.

Grammar:

Arrayobject.join (delimiter)

Parameter description:

Note: Returns a string that strings the elements in the array, with the < delimiter > placed between elements and elements. This method does not affect the original contents of the array. We use the join () method to put all the elements of an array into a string with the following code:
<script type= "Text/javascript" >  var myarr = new Array (3);  Myarr[0] = "I";  MYARR[1] = "Love";  MYARR[2] = "JavaScript";  document.write (Myarr.join ());</script>

Operation Result:

I,love,javascript

We will use separators to separate the elements in the array, with the following code:

<script type= "Text/javascript" >  var myarr = new Array (3)  myarr[0] = "I";  MYARR[1] = "Love";  MYARR[2] = "JavaScript";  document.write (Myarr.join (".")); </script>

Operation Result:

I.love.javascript
Reverse array element order reverse ()

The reverse () method reverses the order of the elements in the array.

Grammar:

Arrayobject.reverse ()

Note: This method changes the original array without creating a new array.

Define the array Myarr and assign the values, and then reverse the order of their elements:

<script type= "Text/javascript" >  var myarr = new Array (3)  myarr[0] = "1"  myarr[1] = "2"  myarr[2] = " 3 "  document.write (Myarr +" <br/> ")  document.write (my arr.reverse() ) </script>

Operation Result:

1,2,33,2,1
Selected element Slice ()

The slice () method returns the selected element from an existing array.

Grammar

Arrayobject.slice (Start,end)

Parameter description:

1. Returns a new array containing the elements from start to end (excluding the element) in the Arrayobject.

2. The method does not modify the array, but rather returns a sub-array.

Attention:

1. Negative values can be used to select elements from the tail of the array.

2. If end is not specified, then the slice () method selects all elements from start to the end of the array.

3. String.slice () is similar to Array.slice ().

We will create a new array and then select the elements from them, the code is as follows:

<script type= "Text/javascript" >  var myarr = new Array (1,2,3,4,5,6);  document.write (Myarr + "<br>");  document.write (Myarr.slice (2,4) + "<br>");  document.write (Myarr);</script>

Operation Result:

1,2,3,4,5,63,41,2,3,4,5,6
Sort Array ()

The sort () method causes the elements in the array to be arranged in a certain order.

Grammar:

Arrayobject.sort (method function)

Parameter description:

1. If the < method function is not specified, it is sorted in Unicode code order.

2. If you specify the < method function, then sort by the < method function > The sorting method specified.

Myarray.sort (SortMethod);

Note: The function compares two values and returns a number that describes the relative order of the two values. The comparison function should have two parameters A and B with the following return values:

A return value of <=-1 indicates that A appears before B in the sorted sequence.
If the return value >-1 && <1, then A and B have the same sort order.
A return value of >=1 indicates that A appears after B in the sorted sequence.

1. Use sort () to sort the array with the following code:

<script type= "Text/javascript" >  var myarr1 = new Array ("Hello", "John", "Love", "JavaScript");   var myarr2 = new Array ("n", "+", "Max", "6", "+", "1");  document.write (Myarr1.sort () + "<br>");  document.write (Myarr2.sort ());</script>

Operation Result:

hello,javascript,john,love1,100,16,50,6,80

Note: The above code does not sort the numbers by the size of the numeric values.

2. To achieve this, you must use a sort function with the following code:

<script type= "Text/javascript" >  function Sortnum (A, a) {  return a-B;//ascending, such as descending, put "A-a" into "b-a"} var Myarr = new Array ("n", "+", "a", "6", "+", "1");  document.write (Myarr + "<br>");  document.write (Myarr.sort (sortnum));</script>

Operation Result:

80,16,50,6,100,11,6,16,50,80,100

Javascript--array Array Objects

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.