The method of array in JS

Source: Internet
Author: User

(Refer to the user example)

One JS does not change the original array method:

    • concat: Concatenate multiple arrays, returning a new array
    • Join: Place all elements in an array with a parameter as a delimiter into one character
    • Slice: Slice (start,end), returns the selected element Array.slice (start, end)

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

        var res = Ary.slice (1,3);

        console.log (ary); //[1,2,3,4,5]    console.log (res); //[2,3]

  • map, does not change the original array ary.map (function(item,index,ary){}) item: Each item index: Index ary: Current array
  • filter, does not change the original array ary.filter (function(item,index,ary){}) item: Index of each index : ARY: Current array
  • ForEach Traversal, does not change the original array ary.foreach (function(item,index,ary){}) item: index per item index ARY: Current array

The method of changing the original array in the second JS:

    • Shift: Delete the first element and return the deleted element, empty is undefined

var ary = [' A ',' B ',' C '];

var res = Ary.shift ();

Console.log (ary); //[' B ', ' C '] Console.log (res); //a

    • Pop: Delete the last one and return the deleted element

var ary = [' A ',' B ',' C '];

var res = Ary.pop ();

Console.log (ary); //[' A ', ' B '] Console.log (res); //C

    • unshift: Adds an element to the beginning of the array and returns the new length

var ary = [' A ',' B ',' C '];

var res = ary. unshift (' d ',' e ');

Console. log (ary); //["D", "E", "a", "B", "C"] console. Log (res); // 5

    • Push: Adds an element to the end of the array and returns the new length

var ary = [' A ',' B ',' C '];

var res = ary. push (' d ',' e ', ' f ');

Console.   log (ary); //["a", "B", "C", "D", "E", "F"] console. Log (res); //6

    • Reverse: Reverse Array order
    • Sort: Sorting an array
    • Splice:Splice (start,length,item) Delete, increment, replace array element, return the deleted array, no delete does not return

Added features

Ary.splice (N,0,x,......,y);

Starting with the index n of the array, deleting 0 items, adding a new item to the front of index N, and the third parameter starting to fill the deleted item location.

var ary = [1,2,3,4,5];var res = ary.splice(1,0,6,7);//从索引值1开始,删除0项console.log(ary); // [1, 6, 7, 2, 3, 4, 5]console.log(res); // [] 删除0项,返回一个空数组

Modified Features
Ary.splice (N,M,X);
Start with the index n of the array, delete the M entry, and add the X to the front of index n

 var ary = [1,2,3,4,5];var res = ary.splice (1,2, 6,7); console.log (ary); //[1, 6, 7, 4, 5]console.log (res); //[2,3]             
 

The method of array in JS

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.