Subtotal array methods in JavaScript

Source: Internet
Author: User

One: An array of common methods:

1:join ();

Converts an array to a string display. The parameter is not entered, the default is a comma connection, and the input parameter is concatenated with the parameter.

var arr=[1,2,3];console.log (Arr.join ());     // ; // 1_2_3; Console.log (arr);           // [n/a];

The original array does not change.

2:reverse ();

The array is sorted in reverse order and the original array is modified.

var arr=[1,2,3]; var arr2=arr.reverse (); Console.log (ARR2);   // [3,2,1]; Console.log (arr);   // [3,2,1];

3:sort ();

By default, array items are sorted in ascending order, the ToString () method of each array item is called, and the resulting string is compared, starting with the first string.

var arr=[2,12,14,21,5];console.log (Arr.sort ());     // [2, 5];

You can also pass in a comparison function as a parameter. If the first argument should be before, the comparison function returns a value less than 0, whereas the comparison function returns a value greater than 0 after the first argument, and if the order does not matter, the comparison function returns 0;

var arr=[2,12,14,21,5];console.log (arr.sort (A, A, b) {return -a));     // [2,5,12,14,21];   var arr1=[2,12,14,21,5];console.log (arr1.sort (A, b) {return b-a}) );    // [21,14,12,5,2];

4:concat ();

The array is merged and the original array is unchanged.

var arr=[1,2,3];console.log (Arr.concat (4,5));            // [1, 2, 3, 4, 5]; Console.log (Arr.concat ([4,5],6));        // [1, 2, 3, 4, 5, 6]; Console.log (Arr.concat ([[4,5],6]));      // [1, 2, 3, [4, 5],6]; Console.log (arr);                        // [1, 2, 3];

5:slice ();

Returns a partial array that contains the array item corresponding to the first argument, but does not contain an array item corresponding to the second parameter. If the passed-in parameter is less than 0, the last entry is 1 from the backward forward number. If only one parameter is passed in, the returned array contains all the elements from the starting position to the end of the array. The original array does not change.

var arr=[1,2,3,4,5];console.log (Arr.slice (1,3));     // [2,3]; Console.log (Arr.slice (1));       // [2,3,4,5]; Console.log (Arr.slice (1,-1));    // [2,3,4]; Console.log (arr);                // [1,2,3,4,5];

6:splice ();

Array concatenation:

1. Delete-Used to delete the element, two parameters, the first parameter (to delete the position of the first item), the second parameter (the number of items to delete);

2. Insert-Inserts any item element into the specified position in the array. Three parameters, first parameter (in fact position), second parameter (0), third parameter (inserted item);

3. Replace-Inserts any item element at the specified position in the array, deleting any number of items, three parameters. First parameter (start position), second parameter (number of items deleted), third parameter (insert any number of items);

Splice () returns an array of deleted elements, or an empty array if no elements are deleted. The original array is modified.

var arr=[1,2,3,4,5,6];console.log (Arr.splice (2));     [3,4,5,6]; Console.log (arr);   [from]; Console.log (Arr.splice (2,0,3,4,5,6));    [];  [1,2,3,4,5,6];       Console.log (Arr.splice (2,2));  [3,4];              Console.log (arr);  [1,2,5,6];        

7:push () and Pop () methods, Unshift () and Shift () methods;

Push () and pop () stack methods, LIFO. The original array has changed.

The push () method adds one or more elements to the end of the array and returns the new length of the array.

The Pop () method deletes the last element of the array, decreases the length of the array, and returns the deleted value.

Unshift () and Shift () queue methods, FIFO. The original array has changed.

The Unshift () method adds one or more elements to the head of the array, changes the index of an existing element, and then returns the new length of the array.

The shift () method deletes the first element of the array and returns it, changing the index of an existing element.

var arr=[1,2,3];console.log (Arr.push (4)); // 4; // [1,2,3,4];   Console.log (Arr.pop ()); // 4; // [n/a]; // 4; // [0,1,2,3]; Console.log (Arr.shift ());   // 0; // [n/a];

8:foreach ();

The first parameter in ForEach () is the element in the collection, the second parameter is the index in the collection, and the third parameter is the collection itself.

9:map ();

The map () runs the given function for each item in the array, returns a list of the results of each function call, and the original array has not been modified.

10:every ()

Runs the given function for each item of an array, and returns True if the function returns true for each item.

11:some ()

Runs the given function for each item of an array, and returns True if the function returns true for either item.

12:filter ()

Each item in an array runs the given function, and returns the list of items that the function returns True.

13:reduce () and reduceright ();

All two methods iterate over all the items of an algebraic group, and then build a value that is ultimately returned. where the reduce () method starts from the first item in the array and traverses through to the end. The Reduceright () begins with the last item in the array and traverses forward to the first item. The array has not been modified.

Two: extension methods

1: Array de-weight

function Unique (array) {    return array.filter (function(item,index) {        return Array.indexof (item) = =index;    })};    var arr=[1,2,3,3,4,2,1,5];console.log (arr);  //[1,2,3,4,5];
functionUnique (arr) {varArr2=[arr[0]], Len=arr.length; if(!Len) {        return; }     for(vari=0;i<len;i++) {Arr2.join (""). IndexOf (Arr[i]) <0?arr2.push (Arr[i]): ""; }    returnarr2;}vararr=[1,2,3,3,4,2,1,5];console.log (Uniq (arr)); //[1,2,3,4,5]

2: Remove empty elements from the array

function Deletenullinarray (array) {    return array.filter (function(item) {          return item!=null;    })  }  var arr=[1,2,null,,, 5];console.log (Deletenullinarray (arr));  //[1,2,5];

Subtotal array methods in JavaScript

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.