javascript--Array

Source: Internet
Author: User

Array

1. definition

A) there is only one-dimensional array in JavaScript, but other ways to implement multidimensional arrays

b) do not need to specify an array type , directly create an array using the new Array ()

c) The data type of the same array can be different, not specify the array size when creating

2. calculate the length of the array

A) get the array length using the Length property directly

b) use var len=array.length;

3. delete the last item of the array

A) Delete

I. Deleting a data at a specified location

Ii.delete ary[location]-- Returns a result of True/false

III. The deleted array length is unchanged, only the data is deleted and no space is freed (wasted space)

b) Splice ()

i. Delete the specified item

Ii.ary.splice (Start(ary.length-1), Length (1))

--- return delete results

Iii. automatically free space after deletion

c) Pop ()

I delete the specified item (last in, first out) using the stack principle

Ii.ary.pop ()-- returns the array result of the default last item

d) Custom method

4. add an item at the end of the array

A) ary[ary.length]=value;--- use arrays starting from 0

b) The principle of using the Push()-- stack to manipulate the last data

i. Ary.push (value)

Ii. Adding an item of a defined and assigned array to the end of another array--ary.push (Ary1[i])

5. Delete the first item of the array (similar to deleting the last item)

A) take the array as a stack, using the shift() function.

I . Deletes the first data of the array and returns the deleted item

  Ii. changing the length of an array

b) Custom method

 I. defining a temporary array --loop to assign a temporary array to the original array , from the data you want to start with the second.

6. Add array first item

A) the array is treated as a stack, using unshift () to delete the first item

      1. Ary.unshift (value1,value2,....)
      2. Returns the result of an array length

b) connect an array using the concat() function

      1. Creates a temporary array and assigns values, joins the temporary array to the original array, and assigns the original array
      2. Ary=temary.concat (ary);

C) Custom method

creates an assigned temporary array, looping over the original array value at the end of the temporary array (temary[temary.length]=ary[i])

7. reverses the contents of an array

A) REverse ()

I. Direct reversal

Ii. returning the result as a reversed array

b) Custom

8. array sorted by character ASCII code Sort()

a) Ary.sort ([Funname])

b) when sort() has no parameters,

i. according to ASCII values are sorted in ascending order (array values are converted to strings and sorted)

II.   when the array value is a number, the results are not necessarily sorted according to the number size ( ASCII less than 89 for example 123 ) )

c) When there is a parameter, the parameter is a custom method name

9. Concatenate the array items with a string

A) purpose of use: to pass data from a page to the background and not to support a type that is combined with a majority of such groups, the user can concatenate the contents of the array with a character (string) as a delimiter, connect the array to a string, and pass the data

b) ary.join (arguments)

I. Connection string customization

II. Results return Connection Results

. get all the contents of an array object

a) Loop traversal

one by one. Multidimensional Arrays

A) JavaScript has only one-dimensional arrays

b) The use of array storage types can be different features

c) storing a one-dimensional array in the data to implement a two-dimensional array

D) Example:

function Person(name,gender)

{

This.name=name;

This.gender=gender;

}

var personary=new Array ();

Personary[0]=new person ("Bill", "man");

Personary[1]=new person ("Lily", "female");

var firstname=personary[0][0];

javascript--Array

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.