JS's Array method sharing

Source: Internet
Author: User

The array in JS occupies a very important position, this article is mainly to share with you JS array method, hope to help everyone.

1. Adding and removing the array the push () method adds one or more elements at the end of the array

  a = [];     A.push ("zero")     //A = ["zero"]     A.push ("One", "one")//A = ["zero", "one", "one"];

Removing an element method at the end of an array is the POP () method, which reduces the length of the array by 1 and returns the deleted element.
2. Join ()

The Array.join () method converts all the elements in an array into strings and joins together, returning the last generated string. The default is a comma, which can be any character in the middle.

var bb = [' abc ', ' CD ', 1,5];     Bb.join ("/")    //"ABC/CD/1/5"

The Array.join () method is the inverse of the String.Split () method, which splits the string into an array.

var str = "ABC/CD/1/5"; Str.split ("/")    //["abc", "CD", "1", "5"]

3, reverse ()

Array.reverse () Reverses the order of the elements in the array,

var s = [n/a];     S.reverse (). Join ("-")   //"3-2-1" 4, sort ()

Sorts the elements in an array, returning the sorted arrays.
When sort () is not with an argument, it is sorted alphabetically.

var a = new Array ("Banaa", "apple", "cherry");     A.sort ();     var s = a.join ("/");   "Apple/banana/cherry"

To sort the array, pass a comparison function, assuming that the first argument is before the comparison function returns a value less than 0,

var a = [33,4,111,222];     A.sort ()   //111,222,33,4     a.sort (function (A, b) {return-A-B});  4,33,222,111

5, Concat ()

The Array.concat () method creates and returns a new array that joins the array element, not the array itself, and concat () does not modify the called array
var a = [n/a];
var B = A.concat (); Copy of array//b = [+/-]
A.concat ([4,5]); [1,2,3,4,5]
6, Slice ()
The Array.slice () method returns a fragment or sub-array of the developed array, where the argument begins and ends

var a = [1,2,3,4,5];var B = A.slice (0,3)  //[1,2,3]a.slice (3)        //[4,5]a.slice (1,-1)      //[2,3,4]a.slice ( -3,-2 )     //[3]

7, Splice ()

The Array.splice () method inserts or removes elements from the array, unlike slice (), concat (), which modifies the array.

  var a = [1,2,3,4,5,6,7,8];     var B = A.splice (4); A = [1,2,3,4],b=[5,6,7,8]     var c = A.slice ($)  //a = [1,4] b=[2,3]     var a = [1,2,3,4,5];     A.splice (2,0, ' A ', ' B ')  //a = [, ' A ', ' B ', 3,4,5]

8. Push (), pop ()

Push () adds one or more elements to the end of the array and returns the new length of the array. Pop () deletes the last element, returning the deleted element.

var stack =[];     Stack.push   //Return 2     stack.pop ()       //Return 2 9, unshift (), Shif ()

In the head of the array, unshift () adds one or more elements to the head, returns the length, Shift () deletes the first element of the array, and returns

var a = [];     A.unshift (1,2,3,4)    //a:[1,2,3,4] Return 4     a.shift ()           //a:[2,3,4]  return 1

Methods for arrays in ES5:

Traverse, map, filter, detect, simplify, search arrays
1. ForEach ()

is to iterate through the array and invoke the specified function for each element, which receives three parameters, the array element (value), index (index), and the array itself (ARR);

var data = [1,2,3,4,5];     Each element value is added 1     data.foreach (function (v,i,a) {         A[i] = v + 1;     })     [2,3,4,5,6]

2. Map ()

The map () method passes each element of the called array to the specified function, returning a new array

A = [n/a];     b = A.map (function (x) {         return x*x;     })     [1,4,9]

3. Filter ()

The filter () method is a logical judgment in the transfer function for each element of an array, and the function returns True, False
var a = [1,2,3,4,5];
var B = a.filter (function (x) {return x < 3})//[1,2]
4, every () and some ()

Every () is the judgment of one of the true,some () that all elements are judged on the transfer function.
var a = [1,2,3,4,5];
A.every (function (x) {return x%2 = = = 0})//false, not all values are even
A.some (function (x) {
return x%2 = = = 0;
})//true,a contains even
5. Reduce () and reduceright ()

Combine array elements to generate a single value
var a = [1,2,3,4,5];
var sum = a.reduce (function (x, y) {return x+y},0)//array sum
var product = a.reduce (function (x, y) {return x*y},1)//Array quadrature
var max = A.reduce (function (x, y) {return (x>y)? X:y})//MAX value
The reduce () function requires two functions, the first being a function that performs a simplified operation, and the second is the initial value.
6, IndexOf () and LastIndexOf ()

Searches for an element of the given value in the entire array, returns the index value of the first element found, does not find return-1,
var a = [0,1,2,1,0];
A.indexof (1)//1
A.lastindexof (1)//3
A.indexof (3)//-1
Es6 Array method
1, Array.of () method, create an array that contains all the arguments

let items = array.of;//[1,2]let items = Array.of (2)  //[2]let items = Array.of ("2")//["2"]

2, Array.from (), converting non-array objects to formal arrays
3, find () and FindIndex () receive two parameters, one is the callback function, the other is an optional parameter, find () returns the value found, Findeindex () returns the index value found,

Let number = [25,30,35,40,45]
Console.log (number.find (n = n > 33))//35
Console.log (number.findindex (n = n >33))//2
Array de-weight

1. Iterate through the array to redo function unique (obj) {    var arr = [];    var len = obj.length;    for (Var i=0;i<len;i++) {        if (Arr.indexof (obj[i]) = =-1) {            Arr.push (Obj[i])        }    }    return arr; Unique ([1,1,1,2,3]) [1,2,3]2, Object key value pair method function unique (obj) {    var tar = {},arr = [],len = Obj.length,val,type;    for (var i = 0;i<len;i++) {        if (!tar[obj[i]]) {            Tar[obj[i]] = 1;            Arr.push (Obj[i])        }    }    return arr; 3, ES6 new Set () method Array.from (new set ([1,2,3,3,3]))//[1,2,3]1.

1. Adding and removing the array the push () method adds one or more elements at the end of the array
a = [];
A.push ("zero")//A = ["zero"]
A.push ("One", "one", "one")//A = ["zero", "one", "one"];
Removing an element method at the end of an array is the POP () method, which reduces the length of the array by 1 and returns the deleted element.
2. Join ()

The Array.join () method converts all the elements in an array into strings and joins together, returning the last generated string. The default is a comma, which can be any character in the middle.
var bb = [' abc ', ' CD ', 1,5];
Bb.join ("/")//"ABC/CD/1/5"
The Array.join () method is the inverse of the String.Split () method, which splits the string into an array.

var str = "ABC/CD/1/5"; Str.split ("/")    //["abc", "CD", "1", "5"]

3, reverse ()

Array.reverse () Reverses the order of the elements in the array,
var s = [n/a];
S.reverse (). Join ("-")//"3-2-1"
4. Sort ()

Sorts the elements in an array, returning the sorted arrays.
When sort () is not with an argument, it is sorted alphabetically.
var a = new Array ("Banaa", "apple", "cherry");
A.sort ();
var s = a.join ("/"); "Apple/banana/cherry"
To sort the array, pass a comparison function, assuming that the first argument is before the comparison function returns a value less than 0,
var a = [33,4,111,222];
A.sort ()//111,222,33,4
A.sort (function (A, b) {return-A-b}); 4,33,222,111
5, Concat ()

The Array.concat () method creates and returns a new array that joins the array element, not the array itself, and concat () does not modify the called array
var a = [n/a];
var B = A.concat (); Copy of array//b = [+/-]
A.concat ([4,5]); [1,2,3,4,5]
6, Slice ()
The Array.slice () method returns a fragment or sub-array of the developed array, where the argument begins and ends

var a = [1,2,3,4,5];var B = A.slice (0,3)  //[1,2,3]a.slice (3)        //[4,5]a.slice (1,-1)      //[2,3,4]a.slice ( -3,-2 )     //[3]

7, Splice ()

The Array.splice () method inserts or removes elements from the array, unlike slice (), concat (), which modifies the array.

var a = [1,2,3,4,5,6,7,8];     var B = A.splice (4); A = [1,2,3,4],b=[5,6,7,8]     var c = A.slice ($)  //a = [1,4] b=[2,3]     var a = [1,2,3,4,5];

A.splice (2,0, ' A ', ' B ')//a = [, ' A ', ' B ', 3,4,5]
8. Push (), pop ()

Push () adds one or more elements to the end of the array and returns the new length of the array. Pop () deletes the last element, returning the deleted element.
var stack =[];
Stack.push ($)//return 2
Stack.pop ()//return 2
9, Unshift (), Shif ()

In the head of the array, unshift () adds one or more elements to the head, returns the length, Shift () deletes the first element of the array, and returns
var a = [];
A.unshift (1,2,3,4)//a:[1,2,3,4] Return 4
A.shift ()//a:[2,3,4] returns 1
Methods for arrays in ES5:

Traverse, map, filter, detect, simplify, search arrays
1. ForEach ()

is to iterate through the array and invoke the specified function for each element, which receives three parameters, the array element (value), index (index), and the array itself (ARR);
var data = [1,2,3,4,5];
Add 1 per element value
Data.foreach (function (v,i,a) {
A[i] = v + 1;
})
[2,3,4,5,6]
2. Map ()

The map () method passes each element of the called array to the specified function, returning a new array
A = [n/a];
b = A.map (function (x) {
return x*x;
})
[1,4,9]
3. Filter ()

The filter () method is a logical judgment in the transfer function for each element of an array, and the function returns True, False
var a = [1,2,3,4,5];
var B = a.filter (function (x) {return x < 3})//[1,2]
4, every () and some ()

Every () is the judgment of one of the true,some () that all elements are judged on the transfer function.
var a = [1,2,3,4,5];
A.every (function (x) {return x%2 = = = 0})//false, not all values are even
A.some (function (x) {
return x%2 = = = 0;
})//true,a contains even
5. Reduce () and reduceright ()

Combine array elements to generate a single value

var a = [1,2,3,4,5];     var sum = a.reduce (function (x, y) {return x+y},0)  //array sum     var product = a.reduce (function (x, y) {return x*y},1)// Array Quadrature     var max = a.reduce (function (x, y) {return (x>y)? X:y})  //MAX     reduce () function requires two functions, the first is a function to perform a simplified operation, The second one is the initial value.

6, IndexOf () and LastIndexOf ()

Searches for an element of the given value in the entire array, returns the index value of the first element found, does not find return-1,

var a = [0,1,2,1,0];     A.indexof (1)  //1     a.lastindexof (1)//3     a.indexof (3)  //-1

Es6 Array method
1, Array.of () method, create an array that contains all the arguments

let items = array.of;//[1,2]let items = Array.of (2)  //[2]let items = Array.of ("2")//["2"]

2, Array.from (), converting non-array objects to formal arrays
3, find () and FindIndex () receive two parameters, one is the callback function, the other is an optional parameter, find () returns the value found, Findeindex () returns the index value found,

Let number = [25,30,35,40,45]
Console.log (number.find (n = n > 33))//35
Console.log (number.findindex (n = n >33))//2
Array de-weight

 1, traversing the array to redo function unique (obj) {var arr = [];    var len = obj.length; for (Var i=0;i<len;i++) {if (Arr.indexof (obj[i]) = =-1) {Arr.push (Obj[i])}} return arr;    Unique ([1,1,1,2,3]) [1,2,3]2, Object key value pair method function unique (obj) {var tar = {},arr = [],len = Obj.length,val,type;            for (var i = 0;i<len;i++) {if (!tar[obj[i]]) {Tar[obj[i]] = 1; Arr.push (Obj[i])}} return arr; 3, ES6 new Set () method Array.from (new set ([1,2,3,3,3]))//[1,2,3]1. 

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.