Array of JavaScript reference types

Source: Internet
Author: User
Tags array length

Array: Each item in the arrays can hold any type of data, and the size of the array is dynamically adjusted (can contain up to 4294967295 items, approximately 4.3 billion items)

[1.1] Array creation:

[1.1.1] Use the array constructor (you can also omit the new operator when using the array constructor)

e.g. var colors = new Array ();

var colors = new Array (20);

var colors = new Array (' Red ', ' blue ', ' green ');

var colors = Array (3);

[note] If a numeric value is passed, an array containing the given number of items is created according to the value;

If you pass a parameter of another type, you create an array of only one item that contains that value.

e.g. var colors = new Array (3);//array with three items

var colors = new Array (' Greg ');//contains an array with an item that is "Greg"

[1.1.2] uses array literal notation (does not call the array constructor with this method)

e.g. var colors = [' Red ', ' blue ', ' green '];

var colors = [];

[Not available] var colors = [1, 2,];

There will be three items in IE8 and in the past, with 1, 2, and undefined arrays for each item. In other browsers, arrays that contain only 1 and 2

[Not available] var colors = [,,,];

An array of 4 items is created in IE8 and previously, and an array of 3 items is created in other browsers

[1.2] Array Read and write

[1.2.1] When reading and setting the value of the array, to use square brackets and provide a corresponding value based on the 0 numeric index, the number of items in the array is saved in its Length property, this property always returns a value of 0 or greater

The length property of the [1.2.2] array can be read-writable by setting the length property of the array to remove the item from the end of the array or to add a new item to the array

e.g. var colors = [' Red ', ' blue ', ' green '];

Colors.length = 2;

Alert (colors[2]);//undefined

Colors.length = 4;

Alert (colors[3]);//undefined

[1.2.3] The length property makes it easy to add new items at the end of the array

e.g. colors[colors.length] = ' black ';

[1.2.4] When a value is placed beyond the size of the array, the array recalculates its length value, that is, the length value equals the index of the last item plus 1

e.g. var colors = [' Red ', ' blue ', ' green '];

COLORS[99] = ' black ';

alert (colors.length);//100

[1.3] Array detection

[1.3.1]if (value instanceof Array) {}: The problem is that it assumes that there is only one global execution environment, and if the Web page contains more than one frame, there are actually more than two different global environments, and there are more than two different versions of the Array constructors. If an array is passed from one frame to another, the incoming array has a different constructor than the array that was created natively in the second frame.

[1.3.2] ECMAScript5 added the Array.isarray () method: if (Array.isarray (value)) {}. The purpose of this method is to ultimately determine whether a value is an array, regardless of the global environment in which it was created

[1.4] Array Conversion

[note] If the value of an item in the array is null or undefined, the value is represented as an empty string in the results returned by the join (), tolocalestring (), toString (), and valueof () methods

[1.4.1]tostring (): Returns a comma-delimited string of strings of each value in the array

[1.4.2]valueof (): Returned or array

e.g. var colors = [' Red ', ' blue ', ' green '];

Console.log (Colors.valueof ());//[' Red ', ' blue ', ' green ']

Alert (colors.valueof ());//' Red,blue,green '

Alert (colors.tostring ());//' Red,blue,green '

alert (colors);//' Red,blue,green ' [note] because alert () receives a string argument, it calls the ToString () method in the background and gets the same result as the ToString () method

[1.4.3]tolocalestring (): It creates a comma-delimited string of array values, and the value of each entry is called the toLocaleString () method

View Code

[1.4.4]join: You can use a different delimiter to build this string, the join receives only one character, is used as a delimiter string, and then returns a string containing all the array items

e.g. var colors = [' red ', ' green ', ' Blue '];

Alert (Colors.join (', '));//' Red,green,blue '

Alert (colors.join (' | | ')); /' red| | green| | Blue

Alert (Colors.join ());//' Red,green,blue '

Alert (colors.join (undefined));//' Red,green,blue ' [note] undefined is used as a delimiter in IE7 and before

[1.5] Array method

[1.5.1] Stack method: Stack is a LIFO (last-in-first-out) LIFO data structure, that is, the most recently added items were first removed. The insertion of items in the stack (called Push-in) and the removal (called ejection) occur only at the top of the stack.

[1.5.1.1]push () Method: You can receive any number of parameters, add them to the end of the array one by one, and return the length of the modified array.

[1.5.1.2]pop () method: Removes the last item from the end of the array, reduces the length value of the array, and then returns the item that was removed.

[1.5.2] Queue method: A queue is a FIFO (first-in-first-out) first-in-one data structure, in which the queue adds items at the end of the list, removing items from the front end of the list.

[1.5.2.1]shift (): Removes the first item in the array and returns the item, with the length of the arrays minus 1 (using shift () and push () to simulate the queue)

[1.5.2.2]unshift (): Add any item to the front of the array and return the new array length (combined with unshift () and pop () to simulate the queue in the opposite direction)

Note IE7 and the following Unshift () methods return always undefined

[1.5.3] Sorting method:

[1.5.3.1]reverse (): Reverses the order of the array, returning the sorted array

[1.5.3.2]sort (): Arranges array items in ascending order, the sort method invokes the ToString () method of each array item, and then sorts the resulting string, returning the sorted array

[Note the]sort () method can accept a comparison function as a parameter to specify which value precedes it. The comparison function receives two parameters, returns a negative number if the first argument should precede the second argument, returns 0 if two arguments are equal, or returns a positive number if the first argument should be after the second argument

[Comparison function] (using: e.g. Array1.sort (compare);)

View Code

For numeric types or the valueof () method returns an object type of numeric type, the comparison function can be simplified to:

function Compare (value1,value2) {

return value2-value1;

}

[Tips]: Use the following methods to create a random array

function Compare () {

Return Math.random ()-0.5;

}

[1.5.4] Operation method (cut, connect, insert, delete, replace):

[1.5.4.1]concat (): Creates a new array based on all items in the current array, creates a copy of the current array, then adds the received parameters to the end of the copy, and finally returns the newly constructed array (concat () does not affect the original array)

[Note 1] When no arguments are passed to the concat () method, it simply copies the current array

[Note 2] If the parameter is one or more arrays, the method adds each item in the array to the result array

[Note 3] If the value passed is not an array, these values are simply added to the end of the result array

e.g. var numbers = [+];

Console.log (Numbers.concat ());//[1,2]

Console.log (Numbers.concat ([5,4,3],[3,4,5],1,2));//[1,2,5,4,3,3,4,5,1,2];

[1.5.4.2]slice (): Creates a new array based on one or more items in the current array, takes one or two arguments, returns the starting and ending positions of the item, and finally returns the new Array (slice () does not affect the original array)

[Note 1] returns the original array when there are no arguments

[Note 2] when there is only one argument, the slice () method returns all items starting at the specified position of the parameter to the end of the current array

[Note 3] Two parameters, the method returns the entry between the start position and the end position, but not the ending position of the item

[Note 4] If the parameter is negative, the array length plus negative number as the parameter

[Note 5] Returns an empty array if the end position is less than the start position

View Code

[1.5.4.3]splice (): The original array becomes the modified array, and splice () returns an array of the items deleted from the original array, and an empty array if no items are deleted

[A] Delete: Two parameters is the position of the first item to be deleted, the number of items to be deleted

[b] Insert: Three parameters are start position, 0 (cardinality to delete), item to insert

[C] Replace: Three parameters are the starting position, the number of items to delete, the item to insert

[Note 1] If the first parameter is a negative number, the array length plus negative number as the parameter

[Note 2] If the second parameter is a negative number, then 0 is used as the parameter

View Code

[1.5.5] Position method (ECMASCRIPT5): Two parameters: The item to find, an index that represents the location of the lookup start (optional). Returns the position of the first find item that satisfies the condition in the array, or 1 if not found (the position method does not affect the original array)

[note] When comparing, use the congruent operator

[1.5.5.1]indexof ()

[1.5.5.2]lastindexof ()

View Code

[Tips] Returns all index values for an item that satisfies a condition

View Code

[1.5.6] Iteration method (ECMASCRIPT5): Two parameters: the function to run on each item, run the function scope object-The value that affects this (optional). The functions passed in these methods receive three parameters: the value of the array item, the position of the item in the array, the array object itself (the iteration method does not affect the original array)

[1.5.6.1]every (): Runs the given function for each item in the array, returns TRUE if the function returns true for each item

[1.5.6.2]filter (): Every item in an array runs a given function, returning the function that returns true of a set of items (commonly used to query all array items that meet the criteria)

[1.5.6.3]foreach (): Runs the given function for each item in the array, this method has no return value (equivalent to a for loop)

[1.5.6.4]map (): Each item in the array runs the given function, returning an array of the results of each function call (commonly used to create an array that contains an item corresponding to another array of one by one)

[1.5.6.5]some (): Runs the given function for each item in the array, and returns True if the function returns true for either item

View Code

[1.5.7] Merge method (ECMASCRIPT5): Iterates over all the items of an algebraic group, building a value that is ultimately returned. Receive two parameters: a function called on each item, the initial value to be the base of the merge (optional). The functions passed to reduce () and reduceright () Accept 4 Parameters: The previous value, the current value, the index of the item, and the array object. Any value returned by this function will be automatically passed to the next item as the first argument. The first iteration occurs on the second item of the array. Therefore, the first parameter is the first item of the array, the second parameter is the second item of the array (the merge method does not affect the original array)

[1.5.7.1]reduce ()

[1.5.7.2]reduceright ()

View Code

Array of JavaScript reference types

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.