JS Array Intermediate + Advanced tips

Source: Internet
Author: User

This paper introduces some methods to compare advanced JS arrays:

reverse: array inversion;

Join: (parameter) stitching an array into a string with a parameter as a connector;


var arr=[];

arr[3]= "haha";

Console.log (Arr.join ("a"));//output: Aaahaha; ARR[3] actually defines an array length of 4 and the first 3 items are undifided, so join ("a") is: null +a+ NULL + a null + A + haha

To implement string inversion with the string split method:

String.prototype.reverse=function () {
Return This.split (""). Reverse (). Join ("");

To generate a cumulative array quickly:

for (var i = 1, arr=[]; Arr.push (i++) <10;);

Console.log (arr);//[1,2,3,4,5,6,7,8,9,10]//change 10 to the desired number

Array sorting:

var arr=[1,13,500,70]

Maximum value: Math.max.apply (Null,arr);//500

Minimum value: Math.min.apply (Null,arr); 1

Sort: from large to small : Arr.sort ((A, b) =>b-a) or Arr.sort (function (A, b) {return b-a});

   from small to large :: Arr.sort ((A, b) =>a-b) or Arr.sort (function (A, b) {return-A-b});

   Random sort arr.sort (function () {return math.random ()-0.5});

  double standard sort :

Example 1: Require arr to be sorted by num size, if NUM is the same, in the new Order of time

var arr= [
{name: ' QQQ ', num:2,time: ' 2015-06-08 13:44:18 '},
{name: ' www ', num:3,time: ' 2015-06-08 13:44:18 '},
{name: ' eee ', num:4,time: ' 2015-06-07 13:40:18 '},
{name: ' RRR ', Num:4,time: ' 2015-06-08 13:44:18 '},
{name: ' yyy ', num:6,time: ' 2015-06-07 13:40:18 '},

Arr.sort (A, b) =>a.num==b.num? Date.parse (b.time)-date.parse (a.time): B.num-a.num)

Example 2: Requirements: Sort arr by the number in each item from small to large

var arr=["That you dasda1222", "Hahaha Das32dasdsa", "Nani 8888dasdsadas", "aaa333"];

Arr.sort (b) =>a.match (/\d/)-B.match (/\d/))

or Arr.sort (function (A, b) {return A.match (/\d/)-B.match (/\d/)})

Array de-weight:

1 Basic version: 

Array.prototype.unique = function () {

Creates a new temporary array to hold the output results
var n = [];

for (var i = 0; i < this.length; i++) {
If the element I of the current array has been saved into a temporary array, skip it, or push the current item into the temporary array
if (N.indexof (this[i]) = =-1) n.push (This[i]);
return n;

2 Efficient version

Array.prototype.unique = function () {
//n is a hash table, r is a temporary array
var n = {}, r = [];

for (var i = 0; i < this.length; i++) {

If there is no current item in the hash table

if (!n[this[i]]) {

N[this[i]] = true;

Push the current item of the current array into the temporary array
R.push (This[i]);
return R;

3 Concise Advanced Edition

Array.prototype.unique = function () {
Return This.filter ((v, i) = = This.indexof (v) = = = = i)


JS Array Intermediate + Advanced tips

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.