JavaScript Basics Grooming--arrays

Source: Internet
Author: User
Tags array definition

JavaScript Basics Grooming -- arrays

1. How to create

empty array:var obj=new array ();

specify an array of lengths: var obj=new array (size);

specify an array of elements : var obj=new array (element 1, Element 2, ...). );

Unit array:var obj=new array[element 1, Element 2, Element 3, ..., element n];

multidimensional array:var a=new array ([array 1],[array 2],[array 3],...,[array n]);

2. Basic Operation

Accessing array elements:

single-dimension array: array name [Subscript index]

Multidimensional array: array name [outer array subscript] [inner array subscript]

Features: The array length is elastic and can be freely shrunk

Array subscript starting from 0

   Subscript Type --value

--Non-numeric--Convert to String

--generating associative arrays

--Subscript as the name of the object property

Array elements can be added to the object

To add an array element: use the [] operator to specify a new subscript

Delete array:Delete array name [subscript]

iterating over an array:for (var array element variable in array);

3. Array Properties

Constructor: constructor that references an array object

Length : Returns the lengths of the array

Prototype: extending an array definition by adding properties and methods

4,ECMAScript 3 method

Add to:

Push ()---Add an array at the end of the array

Unshift ()---Add an array to the head of the array

Concat ()---merging two arrays

Delete:

POP ()---Delete and return the last element of the array

Shift ()---Delete and return the first element of the array

Sub-arrays:

Splice ()---Delete any number of items--Splice (the number of items to be deleted, and to delete);

---Inserts the specified item at the specified location--Splice (starting subscript, 0, item to insert)-----parameter 0 indicates that no items are deleted

---Replace any number of items-->splice (start subscript, number of items to be deleted, item to insert)

Slice ()---Function: Select some elements from an existing array to form a new array

---Slice (returns the starting position of the item, returns the end position of the item)---(1, if it is a negative number, use the array length plus the value to determine the position. 2, the starting position is the actual subscript for the array. 3, the actual subscript of the end position is the end value minus one)

Array sorting:

Reverse ()---Reverses the order of the elements in the array

Sort ()---function: Sorting a character array or array of numbers

---attribute: The default is string comparison

Compare by numeric size (requires function support --Ascending)

function Compare (value1,value2) {if (value1<value2) {return-1;}else{return 0;}
}

  

Array conversions:

ToString ()---converted to a string and returned

Tolocalstring ()---converted to local format and returned

Join ()---Split the array with the specified delimiter and convert to a string

5,ECMAScript 5 method

Location Method:

IndexOf (V1,V2)---Find from start position in array

LastIndexOf (V1,V2)---Search from the end of the array

parameters:v1 The item to find, V2 means to find seven points

Iterative functions:

Every ()---returns True if the function returns true for each item

Filter ()---All array members that return a value of true

ForEach ()---No return value

Map ()---Returns an array of results for each function call

Some ()---any one of the entries returns true, returns True

Parameters: Receive parameters (v1--the function to run on each item, v2--run the function scope object)

The passed in parameter (the value of the v1--array item item,v2--The position of the item in the array, v3--the array object itself)

To reduce the method:

Reduce ()---traverse from the beginning of the array

Reduceright ()---Traversal from the end of the array

Parameters: Receive parameters (v1--function called on each item, v2--as the initial value of the narrowing basis)

incoming parameter (v1--the previous value prev,v2--the current value cur,v3--the index of the item index,v4--array object)

JavaScript Basics Grooming--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.