A concise summary of JavaScript arrays

Source: Internet
Author: User

Defined

var empty=[];

When to use

When the property name is a small, contiguous integer, the data group is used, otherwise the object is used.

Recognition

The typeof operator has no meaning to the data because it returns ' object '
ECMA5 The method has been added
You can add static methods:

array.isarray=function(obj) {    return Object.prototype.toString.apply (obj) = = = ' [ Object Array] '}

Length
The value of the length property is the maximum integer property name of this array plus 1:

Array=[];
array[7]=0;
Console.log (Array.Length);

Setting a larger length does not allocate more space to the array (undefined), while the length setting teenager causes all the subscripts equal to the new length property to be deleted.

Add to

Add a single element:
Empty[empty.length]= "";
Empty.push ("");

Delete

Delete Numbers[2]; Leave a void
Numbers.splice (2,1); Move forward

Find

ie9+ Support
Array.indexof ()
Array.lastindexof ()

Output

Alert ( array.tostring ()), Alert (array.valueof ()), alert (array), alert (Array.join (', '));

Method

    • String method:

Array.concat ()//connection string, equivalent to ' + '
Array.join ()//Generate string

    • Stack method:

Array.push ()
Array.pop ()

    • Queue method

Array.shift ()
Array.unshift ()

    • Reorder Methods

Array.reverse ()//reverse Sort
Array.Sort ()//String sort

    • Operation method

Array.slice ()
The Array.splice ()//is quite powerful and can be used to make arbitrary increases and deletions.

Sort

There is a sort method (Array.prototype.sort) in the data prototype, but only the characters are sorted

functionSortnumber (A, b) {returnAB}vararr =NewArray (6) arr[0] = "10"arr[1] = "5"arr[2] = "40"arr[3] = "25"arr[4] = "1000"arr[5] = "1"document.write (arr+ "<br/>")//character Sortingdocument.write (Arr.sort () + "<br/>")//1,10,1000,25,40,5//Sorting Datadocument.write (Arr.sort (Sortnumber))//1,5,10,25,40,1000

 

Iteration

Only simple usage is described here.//The following only supports ECMA5

// each satisfies a condition that returns true // returns a sub-array that satisfies a condition // operates on each element and does not return a // operates on each element, returns the result array some ()// has a satisfied condition that returns True

Merge

The current latter has a relationship with the implementation of the operation, to facilitate the realization of the function of the two-way list. The following only supports ECMA5

Reduce () reduceright ()

Coding specifications

The following from GitHub, there are a lot of good programming styles about JavaScript, recommend you see: Https://github.com/airbnb/javascript

    • Use the literal syntax for array creation.

      Badvar items =newarray ();//Goodvar items = [];
    • Use Array#push instead of direct assignment to add items to an Array.

      var somestack = [];//badsomestack[somestack.length] = ' abracadabra ';//Goodsomestack.push (' Abracadabra ');
    • When your need to copy a array use Array#slice. JsPerf

      var len = Items.length;var itemscopy = [];var i;//badfor (i =0; i < Len; i++) {  itemscopy[i] = items[i];} Gooditemscopy = Items.slice ();
    • To convert a Array-like object to a array, use Array#slice.

      Functiontrigger () {  var args =array.prototype.slice.call (arguments);  ...}

A concise summary of JavaScript arrays

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.