JavaScript Array Summary

Source: Internet
Author: User
Tags javascript array

1. Create

1     var a = [1,2.1,true, ' abc ']23varnew  Array () 4 5 var New Array (6)7varnew Array (1,2.1,true, ' abc ')         

2. The array is an object, inheriting Array.prototype, you can create a property

Negative and non-integer indexes are automatically converted to strings as properties of the array

Floating-point and integer equal will be converted to integers as index a[1.00] = = A[1]

There is no "out of bounds" issue, just as a property

An array of discontinuous indexes is a sparse array. Arrays that are sparse enough are typically slower to implement than dense arrays, with higher memory utilization, and look for element times like normal object lookup properties

Sparse array omitted elements default undefined,

1 var a1 = [,,,]      in A1     //  false  There is a place to say true, test multiple times is still false2 var A2 = [undefined,,,] in     A2   //true3 a1.length   // 3  Data Direct volume allows an optional trailing comma            

3. When you set length to a nonnegative integer n less than the current length, the values in the current array whose indexes are greater than or equal to n are deleted.

Object.defineproperty (A1, ' length ', {writable:false// Set array length is read-only and cannot be modified
Delete // Array length is not changed, array is thinned after use  in a   //

4. Traverse optimization

1  for (var i=0,len=a.length;i<a;i++) {2     if (!a[i]) Continue ; 3 // dosomething 4 }

5. Js does not support true multi-bit arrays, you can set arrays

6. ES3 defined in the Array.prototype method

1) Join (separator), separated by the specified delimiter to connect all elements, the delimiter is empty when the default is a comma, the reverse operation of String.Split ()

var = [A1]

A1.join (' + ')//1+2+3

2) reverse (), reverses the order of the elements in the array, is not sorted, changes the original array, and does not create a new array.

var a = [3,5,1]

A.reverse ()//A = [1,5,3]

3) sort (A, b) {}), sorts the elements of the array, changes the original arrays, does not create new arrays,

If the method is called without parameters, the elements in the array are sorted alphabetically, more precisely, by the order in which the characters are encoded.

To do this, first convert the elements of the array to a string (if necessary) for comparison.

If you want to sort by other criteria, you need to provide a comparison function that 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:

If a is less than B, a value that is less than 0 is returned if a should appear before B in the sorted array.

If a equals B, 0 is returned.

If a is greater than B, a value greater than 0 is returned.

          var a = [3,5,1]

A.sort (function(A, b) {returnA-})//A = [1,3,5] 

4) concat (item ...), method does not change existing array, create a new array

var a = [+]

Concat (3,[4,[5,6])//[1,2,3,4,[5,6]

5) Slice (start,end), returns a new array containing the start to end (excluding the element), does not change the original array, the position is calculated from the tail when the parameter is negative, end can be omitted

6) Splice (Index,num,item ...), add/Remove elements to the array, and return an array of deleted elements. The method changes the original array, num omits the default deletion to the end

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

var B = A.splice (1,1,[1], ' B ')//a=[1, [1], ' B ', 3,4] b=[2]

Unlike concat (), splice inserts the input itself

7) Push (), add one or more elements to the end of the array, return the new length of the array

8) Pop (), delete the last element of the array, reduce the length of the array and return the deleted value

9) Unshift (), similar to push (), but added from the head

var a= [up]

A.unshift (4,5)//[4,5,1,2] elements are added at once, not individually

Shift (), similar to pop (), removed from the head

ToString (), and the join () with no parameters returns the same value

7. Array methods in ES5

Most methods The first parameter receives a function that is called once for each element in the array, and the element that does not exist in the sparse function is not called.

This parameter function has three parameters: the array element, the index, the array itself, usually only the first one

1) ForEach (function (Value,index,arr) {}), traversing from beginning to end, all elements cannot be terminated without traversing, unless placed in try: Catch and throw an exception

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

var sum = 0

A.foreach (function (value) {

Sum + = value//additive, final sum=10

})

2) Map (function (Value,index,arr) {}), do not modify the original array, return a new array, and the original array has the same length

var a = [1,,3]

var B = a.map (function (value) {

Return Value*value

})//b = [1,,9]

3) filter (function (Value,index,arr) {}), do not change the original array, return the call array of a self, passed the function used for logical judgment, if the true element will be added to the subset,

The values that do not exist in the sparse function are skipped, so the return values are dense

var dense = sparse.filter (function () {return true})//Compress sparse function, return dense

4) every () returns True when all elements return true for the passed function, False if one returns false, and returns false immediately.

Sparse array does not exist element does not traverse

5) Some () returns True when one of the elements returns true for the passed function, or false if one of them returns true immediately stops the traversal and returns TRUE.

Sparse array does not exist element does not traverse

6) Reduce (function (x, y), default), combine array elements with the specified function to generate a single value

The first time you call function, X is default, and then X is the last return value

If default is not specified, X, Y is the first and second element of the array, and an error is given if the arrays are empty

7) The Reduceright method is similar to reduce, just as the array index is processed from high to low

8) IndexOf (Val), looking for and returning values in the array index, does not exist return-1

9) LastIndexOf (Val), starting at the end of the search and returning the value of the index in the array, does not exist return-1

8. The judging variable is the array var a = []

Bst

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

return typeof o = = = ' object ' && Object.prototype.toString.call (o) = = = ' [Object Array] '

}

ES5

Array.isarray (a)

9. Class array objects, such as arguments, do not inherit array.prototype and cannot use the array method directly, but can be passed Function.call ()

var a = {' 0 ': ' A ', ' 1 ': ' B ', length:2}

Array.prototype.join.call (A, ' + ')//' A+b '

In Firefox can be abbreviated to Array.join (A, ' + '), but not all browsers can, so you can modify the definition

Array.join = Array.join | | function (A.SEP) {

Return Array.prototype.join.call (A,SEP)

}

String similar to read-only group in ES5

var s = ' JavaScript '

Console.log (Array.prototype.join.call (S, "))//J A V A s C r i p t

Search online, Streamline maps

JavaScript Array Summary

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.