Several methods used in arrays

Source: Internet
Author: User

Push: Adding a return value to the end of the array is the new length of the array

Unshift: Adding a return value to the beginning of the array is the new length of the array
Pop: Delete the end item of an array the return value is a deleted array item
Shift: Delete Array start item returns the beginning item that was deleted
Splice: Deleting any item in an array returns a value that is a deleted array item
Slice: Copy array return value is copied to the new array after the value is written does not contain the last item being copied

Stitching:
Concat: Stitching an array together with another array to return a well-stitched array
Join: Stitch Each item in the array into a string by the specified delimiter

Sort:
Reverse: Reverse Array return value reverse array original array change
Sort: Bubble sort based on anonymous function b-a reverse A-B ascending

Poor compatibility:
INDEXOF: Returns the index of the get item in the array
LastIndexOf: Returns the last index that gets the item that appears in the array
ForEach: Looping through an array parameter is an anonymous function returned by default to undefined
Map: Looping through an array parameter is an anonymous function

On the code:

var ary = [1,2,3,4,5,6,7,8,9,0];    var ary1=[1,5,7,4];    var ary2=[2,3,4,5,7,3,4,5];    var a=ary.splice (2,3,5);//delete any of the items in the array can be extended to push,unshift,pop,shift,slice most commonly used var b=ary.splice (2,1);    /*var a= "Test";            var b=[123]; Used for the foreach Test */var C=ary.push (1);//Add a var d=ary.unshift (3) at the end of the array;//Add a var e=ary.pop () at the beginning of the array;//delete an item at the end of the array V Ar f=ary.shift ();//Delete a var g=ary.slice (2,5) at the beginning of the array;//copy array var h=ary.concat (Ary1,ary2). Concat (1,2,2,2,2,[456654]); If there is no parameter or the parameter is () NULL, the assignment array is var i=ary.join (' + ');//each item in the array is spliced with the specified delimiter into a new string Var j=ary.reverse ();//Reverse sort the original array to change Var k=    Ary1.sort (function (A, b) {return b-a;}); var l=ary2.indexof (4);//Returns the index that gets the item that appears in the array var m=ary2.lastindexof (4);//Returns the last index that gets the item in the array var n=ary2.indexof (9);//If the Get item is not    Present returns-1 Var o=ary.foreach (function (A, b) {Console.log (A, A, b)});    var p=ary2.map (function () {});    Console.log (a);    Console.log (b);    Console.log (c);    Console.log (d);    Console.log (e);    Console.log (f);    Console.log (g); ConsoLe.log (h);    Console.log (i);    Console.log (j);    Console.log (k);    Console.log (l);    Console.log (m);    Console.log (n);//Console.log (A,XB);    Console.log (o);    Console.log (ary); Console.log (Ary1);
Splice's expanded use:
Analog Push Ary.splice (ary.length,0,x)
Analog pop Ary.splice (ary.length-1,1)
Analog Unshift Ary.splice (0,0,x)
Analog shift Ary.splice (0,1)
Splice (0) from 0 to the end ==> all deleted operations ==> return all array items ==> cloned array

Several methods used in 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.