Hyper-Detailed JavaScript array method Rollup _javascript tips

Source: Internet
Author: User
Tags javascript array

First, the common method of array
1:join ();

Converts an array to a string display. No parameters are entered, the default is a comma connection, and the input parameter is connected by a parameter.

var arr=[1,2,3];
Console.log (Arr.join ());  1,2,3;
Console.log (Arr.join ("_")); 1_2_3;
Console.log (arr);      [1,2,3];

The original array is unchanged.

2:reverse ();

Arranges the array 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, the array items are sorted in ascending order, the ToString () method of each array item is invoked, and the resulting string is compared, starting at the top of the string.

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

You can also pass in a comparison function as an argument. If the first argument should be before, the comparison function returns a value less than 0, and the comparison function returns a value greater than 0 when the first argument is followed, or the comparison function returns 0 if the order does not matter;

var arr=[2,12,14,21,5];
Console.log (Arr.sort (function (a,b) {return a-b}));   [2,5,12,14,21]; 

var arr1=[2,12,14,21,5];
Console.log (Arr1.sort (function (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 entries for the first argument, but does not contain an array item corresponding to the second argument. If the incoming argument is less than 0, the number is from the back forward and the last is-1. If only one argument is passed in, the returned array contains all the elements from the start position to the end of the array. The original array is unchanged.

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-To delete the element, two parameters, the first argument (to delete the position of the first item), the second argument (number of items to delete);

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

3. Replace-inserts any item element into the array at the specified position, deleting any number of items, and three parameters. The first argument (the starting position), the second argument (the number of items deleted), and the third parameter (inserts any number of items);

Splice () returns an array of deleted elements, or returns an empty array if the element is not deleted. The original array was modified.

var arr=[1,2,3,4,5,6];
Console.log (Arr.splice (2));  [3,4,5,6];
Console.log (arr);    [1,2];
Console.log (Arr.splice (2,0,3,4,5,6));  // [];
Console.log (arr); [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 first. 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, reduces the length of the array, and returns the deleted value.

Unshift () and Shift () queue method, advanced first out. The original array has changed.

The Unshift () method adds one or more elements to the head of an 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, and changes the index of the existing element.

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

8:foreach ();

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

9:map ();

Map () Each item in the array runs the given function, returning an array 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 the array, and returns True if the function returns true for each item.

11:some ()

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

12:filter ()

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

13:reduce () and reduceright ();
Two methods will iterate over all the items of the group, and then build a final return value. The reduce () method begins with the first item of the array, traversing to the last. Reduceright () begins with the last item in the array and traverses forward to the first item. The array has not been modified.

Second, the extension method
1: Array to Heavy

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 (Unique (arr)); [1,2,3,4,5];

function Unique (arr) {
  var arr2=[arr[0]],
    len=arr.length;
  if (!len) {return
    ;
  }
  for (Var i=0;i<len;i++) {
    arr2.join (""). IndexOf (Arr[i]) <0?arr2.push (arr[i)): "";
  }
  return arr2;
}
var arr=[1,2,3,3,4,2,1,5];
Console.log (Uniq (arr)); [1,2,3,4,5]    

2: Remove the 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];

Related Article

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.