An array of the JavaScript authoritative guides

Source: Internet
Author: User

First, create an array

var a = [,,]; Array has 2 elements

var a = new Array (10); Specify length

Second, sparse array

An array of non-contiguous indexes starting at 0. You can use the array () constructor or simply specify that the index of the array is greater than the array length to create.

var a1 = [,,,]; Array is [undefined, undefined, undefined]

var a2 = new Array (3); No element

Delete array element does not modify length, array becomes sparse array

Three, array traversal

function Traversal (o) {

var keys = Object.keys (o);

var values = [];

for (var i = 0;

i < keys.length; i++) {

var key = Keys[i];

Values[i] = O[key];

}

return values;

}

Skipping null undefined elements that do not exist

if (!a[i]) continue;

Skip undefined elements that do not exist

if (a[i] = = = undefined) continue;

Skip only elements that don't exist

if (! I in a) continue;

Handling sparse matrices with for

Each traversal assigns an enumerable property name to the loop variable

var a = [1,,2,3];

var b = [];

var pos = 0;

For (var index in a) {

b[pos++] = A[index];

}

Iv. Multidimensional Arrays

var table = new Array (10);

for (var i = 0;

i < table.length; i++) {

Table[i] = new Array (10);

}

Initialization

for (i = 0; i < table.length; i++) {

for (J=0; J < Table[i].length; J + +) {

TABLE[I][J] = i * j;

}

}

V. Array method Join ()

Converts all elements of an array to strings together, returns the resulting string, can specify a string to separate elements, and does not specify a comma using the default

Reverse ()

Reverses the order of the elements in the array, returning an array of reverse. Rearrange them in the original array

Sort ()

Sorts the elements of the array and returns the sorted array, which is sorted in alphabetical order without an array call, and if it contains undefined, it is queued to the end of the array.

Pass a comparison parameter to sort (). Assuming that the first argument should be before, the comparison function returns a value less than 0.

A.sort (function (A, b) {return-A-b}); Numeric size

A.sort (function (b) {

var c = a.tolowercase ();

var d = b.touppercase ();

if (c < d) return-1;

if (C > D) return 1;

});

Concat ()

Creates and returns a new array whose elements include the original array called concat () and the supplied arguments. If there is an array, the elements of the array are connected.

Slice ()

Returns the fragment or sub-array of the specified array, with two parameters specifying the starting and ending positions (not included). A negative number indicates that the array is not modified relative to the corresponding position of the last element.

Splice ()

You can delete an element from an array, insert an element, and the array element changes the index value after the operation is completed. The first parameter specifies the starting position, the second parameter specifies the number of elements removed from the element, and if the second argument is omitted, all are removed from the starting position. The third parameter begins by specifying the element to insert at the position specified by the first parameter.

Push () Pop ()

Use the array as a stack to return the new length of the array

Unshift () Shift ()

Unshift () Adds or removes elements at the head of the array, moves the remaining elements forward, and returns the new length of the array. Using the multi-parameter call Unshift () is a one-time insert shift () to delete the first element of the array and return, moving the remaining elements down

ToString () tolocalestring ()

ToString () converts each element to a string, separated by commas

Vi. Array methods for ECMASCRIPT5

ForEach () traversal

Invokes the specified function for each parameter. To terminate prematurely you need to put a foreach () in a try and throw an exception.

Map () Map

Passes each element to the specified function, returning an array containing the return value of the function. The function passed to map () should have a return value. Returns a new array, without modifying the original array

var a = [n/a];

b = A.map (function (x) {return x*x;});

Filter () Filtering

Returns a subset of elements that are added to the returned array if the return value is true or can be converted to true. The missing elements of the sparse array are skipped.

var a = [1,2,3,4,5];

A.filter (function (x) {return x%2==0});

Every () some ()

Detect every () returns true if and only if all elements of the array call the function return true some () if at least one of the element call functions returns True, return True if every () or some () stops traversing when determining what value to return

Reduce () Reduceright () simplifies

Uses the specified function to combine array elements to produce a single value. The first parameter combines or converts two values to a value that returns the reduced value. The second argument (optional) is passed to the initial value of the function. Reduceright () follows the array index from right to left

IndexOf () lastIndexOf ()

The search searches for the element of the given value, returns the index of the first element found, and returns -1,lastindexof () from the last start. The second argument (optional) indicates that it can be a negative number, starting at that position. Returns the index of all element matching values in the array

function FindAll (A, x) {

var results = [],

Len = A.length,

pos = 0;

while (Pos < len) {

pos = A.indexof (X,pos);

if (pos = = =-1) break;

Results.push (POS);

pos = pos + 1;

}

return results;

}

VII. Detecting arrays

var IsArray = Function.isarray | | Function (o) {

return typeof o = = = "Object" &&

Object.prototype.toString.call (o) = = = "[Object Array]";

}

An array of the JavaScript authoritative guides

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.