// s is 'a/b/c'
var s = ['a', 'b', 'c'].join('/');
Remove element from the array
var a = [1, 2, 3, 4, 5];
// a is [1, 2, 5], b is [3, 4]
var b = a.splice(2, 2);
Remove last element from the array
var a = [1, 2, 3, 4, 5];
// last is 5 and a is [1, 2, 3, 4]
var last = a.pop();
Remove first element from the array
var a = [1, 2, 3, 4, 5];
// first is 1 and a is [2, 3, 4, 5]
var first = a.shift();
Add element to the end of the array
var a = [1, 2, 3, 4, 5];
// a is [1, 2, 3, 4, 5, 0]
a.push(0);
Add element to the beginning of the array
var a = [1, 2, 3, 4, 5];
// a is [0, 1, 2, 3, 4, 5]
a.unshift(0);
Get subarray of the array
var a = [1, 2, 3, 4, 5];
// suba is [ 3, 4 ]
var suba = a.slice(2, 4);
Replace subarray of the arary with another array
var a = [1, 2, 3, 4, 5];
// a is [1 ,2, 'a', 'b', 'c', 5]
a.splice(2, 2, 'a', 'b', 'c');
Iterate over all elements of the array
for (int i = 0; i < array.length; i++) {
// handle array[i]
}
Get unique elements of the array
Array.prototype.unique= function(){
var u = {}, a = [];
for(var i = 0, l = this.length; i < l; ++i){
if(u.hasOwnProperty(this[i])) {
continue;
}
a.push(this[i]);
u[this[i]] = 1;
}
return a;
}
// a is [ 1, 2, 3, 4, 5 ]
var a = [1, 2, 2, 2, 3, 4, 5, 5].unique();
Get difference between two arrays
Array.prototype.diff = function(a) {
return this.filter(function(i) {return !(a.indexOf(i) > -1);});
};
// a is [1, 5]
var a = [1, 2, 3, 4, 5].diff( [2, 3, 4, 10, 11] );
Get intersection of two arrays
Array.prototype.intersection = function(a) {
return this.filter(function(i) {return (a.indexOf(i) > -1);});
};
// a is [2, 3, 4]
var a = [1, 2, 3, 4, 5].intersection( [2, 3, 4, 10, 11] );
Append one array to another
// a is [ 1, 2, 2, 3 ]
var a = [1, 2].concat([2, 3]);
Reverse the array
// a is [3, 2, 1]
var a = [1, 2, 3].reverse();
Find all elements of the array that pass test
// find all even numbers
// a is [0, 2, 4]
var a = [0, 1, 2, 3, 4].filter(function(n) {
return ((n % 2) === 0);
});
Sort the array numerically
// descending order
// a is [12, 11, 3, 2, 1]
var a = [11, 12, 1, 2, 3].sort(function(a,b){return b-a});
// ascending order
// a is [1, 2, 3, 11, 12]
var a = [11, 12, 1, 2, 3].sort(function(a,b){return a-b});
Sort the array lexically
// ascending order
// arr is ['One', 'Three', 'Two']
var arr = ['One', 'Two', 'Three'].sort(function(a,b){return a.localeCompare(b)});
// descending order
// arr is [ 'Two', 'Three', 'One' ]
var arr = ['One', 'Two', 'Three'].sort(function(a,b){return b.localeCompare(a)});
Shuffle the array
function shuffle(array) {
var counter = array.length, temp, index;
while (counter > 0) {
index = Math.floor(Math.random() * counter);
counter--;
temp = array[counter];
array[counter] = array[index];
array[index] = temp;
}
return array;
}
var a = shuffle([0, 1, 2, 3, 4, 5]);
Map elements of the array
// a is [0, 1, 4, 9, 16, 25]
var a = [0, 1, 2, 3, 4, 5].map(function(a) {return a*a});
Check if the array contains the element
var hasItem = (a.indexOf(obj) !== -1);
Check whether all elements in the array pass the test
// allEven is false, 1 and 3 are odd
var allEven = [0, 1, 2, 3, 4].every(function(n) {return ((n % 2) === 0);});
// allEven is true
var allEven = [0, 2, 4].every(function(n) {return ((n % 2) === 0);});
Check whether at least one element in the array passes the test
// someEven is true
var someEven = [0, 1, 2, 3, 4].some(function(n) {return ((n % 2) === 0);});
Reduce array elements (left-to-right)
// sum is 10
var sum = [0,1,2,3,4].reduce(function(previousValue, currentValue, index, array){
return previousValue + currentValue;
});
Reduce array elements (right-to-left)
// r is '43210'
var r = [0,1,2,3,4].reduceRight(function(previousValue, currentValue, index, array){
return previousValue + currentValue.toString();
});
Perform function on every element of the array
// just print all elements
['One', 'Two', 'Three'].forEach(function(obj){
console.log(obj);
});
Find maximum number in the array
Array.max = function( array ){
return Math.max.apply( Math, array );
};
// m is 10
var m = Array.max([-1, 10, 0]);
Find minimum number in the array
Array.min = function( array ){
return Math.min.apply( Math, array );
};
// m is -1
var m = Array.min([-1, 10, 0]);