2015-03-22--Common JS Array method

Source: Internet
Author: User

Array

Array.concat (item ...); produce a new array
If item is an array, then each element of it is added separately (shallow copy, only one layer is parsed).
Example:
var a = [1, 3, 4];
var B = [5, 8];
var C = A.concat (b, true, false);
Console.log (a);
Console.log (b);
Console.log (c);
=>1, 3, 4
=>5, 8
=>1, 3, 4, 5, 8, True, False

var a = [1, 3, 4];
var b = [[9, 9], 8];
var C = A.concat (b, true, false);
Console.log (a);
Console.log (b);
Console.log (c);
=>1, 3, 4
=>[9, 9], 8
=>1, 3, 4, [9, 9], 8


Array.join (separator); The elements in the array are concatenated into strings with separator. Separator default ', '. The efficiency is better than the + number hyphen in IE6/7.
Example:
var a = [1, 3, True, 4];
Console.log (A.join ("));
Console.log (A.join (' | '));
Console.log (a);
=>13true4
=>1|3|true|4
=>1, 3, True, 4


Array.slice (start, end); Shallow copy, [Start, end] open left and right. End is array.length by default, and if start or end is negative, Array.Length tries to add it to it.
Example:
var a = [5, 3, 4, 8];
var B = A.slice (1, 3);
var C = A.slice (-2,-1);
var d = A.slice (-2);
Console.log (a);
Console.log (b);
Console.log (c);
Console.log (d);
=>5, 3, 4, 8
=>3, 4
=>4
=>4, 8


Array.splice (Start, DeleteCount, item ...); Remove one or more elements and replace them with a new item. Returns an array containing the elements that were removed.
Simulation implementation:
Function.prototype.method = function (name, func) {
This.prototype[name] = func;
return this;
};
Array.method (' Splice ', function (start, deletecount) {
var len = this.length;
var insertcount = Math.max (arguments.length-2, 0);
var gap = Insertcount-deletecount;
var Newlen = this.length + gap;
var delarr = [];
var delcounttemp = DeleteCount;
var i = 0;
var j = 0;
var k;
Start = Start | | 0;
if (Start < 0) {
Start = start + len;
}
Start = Math.max (Math.min (Start, Len), 0);
while (Delcounttemp > 0) {
Delarr[i] = This[start + i];
i++;
delcounttemp--;
}
K = len-1-(start + deleteCount-1); The number of elements in the original array that need to move positions
if (Gap > 0) {
while (k > 0) {
This[len-1 + gap-j] = this[len-1-j];
j + +;
k--;
}
Console.log (Newlen);
This.length = Newlen;
} else if (Gap < 0) {
while (k > 0) {
This[start + deleteCount-1 + gap + j + 1] = This[start + deleteCount-1 + j + 1];
j + +;
k--;
}
This.length = Newlen;
}
for (i = 0; i < Insertcount; i++) {
This[start + i] = arguments[i + 2];
}
return Delarr;
});
Example:
var a = [1, 3, 4, 5];
var B = A.splice (1, 1, [4], ' AA ');
Console.log (a);
Console.log (b);
=>1, [4], ' AA ', 4, 5
=>3


Array.push (item ...); Adding an element at the end of the array modifies the original array to return the array length. When item is an array, it is treated as an element.
Simulation implementation:
Function.prototype.method = function (name, func) {
This.prototype[name] = func;
return this;
};
Array.method (' push ', function () {
This.splice.apply (This, [This.length-1, 0].concat (Array.prototype.slice.apply (arguments)));
return this.length;
});
Example:
var a = [1, 3, 4];
var B = [5, 8];
var C = A.push (b, true, false);
Console.log (a);
Console.log (b);
Console.log (c);
=>1, 3, 4, [5, 8], true, false
=>5, 8
=>6


Array.pop (); Removes the last element in an array and returns the element.
Simulation implementation:
Function.prototype.method = function (name, func) {
This.prototype[name] = func;
return this;
};
Array.method (' Pop ', function () {
Return This.splice (this.length-1, 1) [0]; Based on splice implementation
});
Example:
var a = [1, 3, True, 4];
var B = A.pop ();
Console.log (a);
Console.log (b);
=>1, 3, True
=>4


Array.unshift (item ...); Adds an element to the head of the array, modifies the original array, and returns the array length. When item is an array, it is treated as an element. The return value in IE6, which is always undefined.
Simulation implementation:
Function.prototype.method = function (name, func) {
This.prototype[name] = func;
return this;
};
Array.method (' Unshift ', function () {
This.splice.apply (this, [0, 0].concat (Array.prototype.slice.apply (arguments)));
return this.length;
});
Instance:
var a = [1, 3, 4];
var B = [5, 8];
var C = A.unshift (b, true, false);
Console.log (a);
Console.log (b);
Console.log (c);
=>[5, 8], True, False, 1, 3, 4
=>5, 8
=>6


Array.shift (); Moves the first element in an array and returns the element. In general, shift is much slower than pop.
Simulation implementation:
Function.prototype.method = function (name, func) {
This.prototype[name] = func;
return this;
};
Array.method (' Shift ', function () {
Return This.splice (0, 1) [0]; Based on splice implementation
});
Example:
var a = [5, 3, 4];
var B = A.shift ();
Console.log (a);
Console.log (b);
=>3, 4
=>5


Array.reverse (); Reverses the array, returning the current array.
Example:
var a = [1, 3, 4];
var B = A.reverse ();
Console.log (a);
Console.log (b);
=>4, 3, 1
=>4, 3, 1


Array.Sort (COMPAREFN); The elements that are sorted by default are strings. You can replace the default comparison function with a custom comparison function. Returns 0 if the two parameters are equal. If the first argument should be before, a negative number is returned. If the second argument should be before, a positive number is returned.
Example:
var a = [5, 6, ' + ', ' a ', ' a ', ' Ade ', ' Ade '];
var B = A.sort (); Sort by string
Console.log (a);
Console.log (b);
= ' to ', 5, 6, ' a ', ' a ', ' Ade ', ' Ade '
= ' to ', 5, 6, ' a ', ' a ', ' Ade ', ' Ade '

var a = [3, 5, 11, 8, 33];
var B = a.sort (function (A, B) {//Sort numbers
return a-B;
});
Console.log (a);
Console.log (b);
=>3, 5, 8, 11, 33
=>3, 5, 8, 11, 33

var a = [5, 6, ' 3 ', ' A ', ' a ', ' Ade ', ' Ade '];
var B = a.sort (function (A, B) {//Sort numbers and strings
if (a = = b) {
return 0;
} else if (typeof a = = = typeof b) {
Return a < b? -1:1;
} else {
Return typeof a < typeof B? -1:1;
}
});
Console.log (a);
Console.log (b);
=>5, 6, ' 3 ', ' A ', ' a ', ' Ade ', ' Ade '
=>5, 6, ' 3 ', ' A ', ' a ', ' Ade ', ' Ade '

var a = [
{' First ': 5, ' second ': ' d '},
{' First ': ' DD ', ' Second ': 44},
{' First ': 3, ' second ': ' E2 '},
{' First ': ' 3a ', ' Second ': 3}
];
var by = function (name) {//Sorts the object, the Sort method is unstable (the relative position of the two equal elements is not fixed), so the chain calls the by method more than once, not guaranteeing the desired result. You can use the enhanced version below to get the results you want.
return function (A, b) {
var temp1, Temp2;
if (typeof a = = = ' object ' && typeof b = = = ' object ' && a && b) {
Temp1 = A[name];
Temp2 = B[name];
if (Temp1 = = = Temp2) {
return 0;
} else if (typeof Temp1 = = = typeof Temp2) {
Return Temp1 < Temp2? -1:1;
} else {
Return typeof Temp1 < typeof Temp2? -1:1;
}
} else {
throw {
' Name ': ' sort of an error! ‘,
' Message ': ' array contains elements of type object '
};
}
};
};
var B = A.sort (by (' first '));
Console.log (a);
Console.log (b);
=>{' first ': 3, ' second ': ' E2 '},
{' First ': 5, ' second ': ' d '},
{' First ': ' 3a ', ' Second ': 3},
{' First ': ' DD ', ' Second ': 44}
=>{' first ': 3, ' second ': ' E2 '},
{' First ': 5, ' second ': ' d '},
{' First ': ' 3a ', ' Second ': 3},
{' First ': ' DD ', ' Second ': 44}

var a = [
{' First ': 5, ' second ': ' d '},
{' First ': ' DD ', ' Second ': 44},
{' First ': 3, ' second ': ' E2 '},
{' First ': 3, ' Second ': 3}
];
var by = function (name, minor) {//minor is a secondary comparison function, called when A and B are equal.
return function (A, b) {
var temp1, Temp2;
if (typeof a = = = ' object ' && typeof b = = = ' object ' && a && b) {
Temp1 = A[name];
Temp2 = B[name];
if (Temp1 = = = Temp2) {
return typeof minor = = = ' function '? Minor (A, b): 0;
} else if (typeof Temp1 = = = typeof Temp2) {
Return Temp1 < Temp2? -1:1;
} else {
Return typeof Temp1 < typeof Temp2? -1:1;
}
} else {
throw {
' Name ': ' sort of an error! ‘,
' Message ': ' array contains elements of type object '
};
}
};
};
var B = A.sort (by (' first '));
Console.log (a);
Console.log (b);
=>{' first ': 3, ' Second ': 3},
{' First ': 3, ' second ': ' E2 '},
{' First ': 5, ' second ': ' d '},
{' First ': ' DD ', ' Second ': 44}
=>{' first ': 3, ' Second ': 3},
{' First ': 3, ' second ': ' E2 '},
{' First ': 5, ' second ': ' d '},
{' First ': ' DD ', ' Second ': 44}

2015-03-22--Common JS Array method

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.