MENU
Prototype Methods – Mutators
These mutator functions modify the original array.Single-element Addition/Removal:Single-element Addition/Removal: Single-element Addition/Removal:
Array.prototype.push()
Adds one or more elements to the end of an array and returns the new length of the array.
Array.prototype.pop()
Removes the last element from an array and returns that element.
Array.prototype.unshift()
Adds one or more elements to the front of an array and returns the new length of the array.
Array.prototype.shift()
Removes the first element from an array and returns that element.
RESETRUNFULL
<!DOCTYPE html><html><body><script>
var a = [3];
a.unshift(1,2);
a.push(4,5,6);
console.log(a.shift()); // 1
console.log(a.pop()); // 6
console.log(a); // [2,3,4,5]
</script></body><html>
This extends an array with another array, without creating a new array. |
a.push.apply(a, b) |
Multiple-elements Manipulation: Multiple-elements Manipulation: Multiple-elements Manipulation:
Array.prototype.copyWithin()
Copies a sequence of array elements within the array.
Array.prototype.fill()
Fills all the elements of an array from a start index to an end index with a static value.
Array.prototype.splice( start, deleteCount, item1, item2, ... )
Adds and/or removes elements from an array.
RESETRUNFULL
<!DOCTYPE html><html><body><script>
var a = [0,1,2,3,4,5,6];a.fill('x',5,8); // [0,1,2,3,4,'x','x']Array(10).fill('x'); // ["x", "x", "x", "x", "x", "x", "x", "x", "x", "x"]a.splice(2,3); // [0,1,'x','x']a.splice(2,2,9,9); // [0,1,9,9]a.unshift(1,2); // [1,2,0,1,9,9]a.push(4,5,6); // [1,2,0,1,9,9,4,5,6]console.log(a); // [1,2,0,1,9,9,4,5,6]console.log(a.shift()); // 1console.log(a.pop()); // 6console.log(a); // [2,3,4,5] // .copyWithin(a,b=0,c=arr.length) means shallow-copies // elements b to c(excluding c) to location a. Negative // parameters are relative to the end.[1, 2, 3, 4, 5].copyWithin(-2); // [1, 2, 3, 1, 2][1, 2, 3, 4, 5].copyWithin(0, 3); // [4, 5, 3, 4, 5][1, 2, 3, 4, 5].copyWithin(0, 3, 4); // [4, 2, 3, 4, 5][1, 2, 3, 4, 5].copyWithin(-2, -3, -1); // [1, 2, 3, 3, 4][].copyWithin.call({length: 5, 3: 1}, 0, 3); // {0: 1, 3: 1, length: 5}
</script></body><html>
RESETRUNFULL
<!DOCTYPE html><html><body><script>
var a = ['a','b','c'];delete a[0];a.splice(1,1);console.log(a); // [undefined,"c"]
</script></body><html>
Ordering: Ordering: Ordering:
Array.prototype.reverse()
Reverses the order of the elements of an array in place — the first becomes the last, and the last becomes the first.
Array.prototype.sort()
Sorts the elements of an array in place and returns the array. This function takes as the parameter another comparison function, which in turn takes two parameters, a and b. The comparison function returns a negative number if a comes first, and a positive number if the reverse is true. 0 is returned by it if a and b are equal.
RESETRUNFULL
<!DOCTYPE html><html><body><script>
var a = [0,1,2,3,4,5,6];
console.log(a.reverse().sort()===a); // true
console.log(a.sort((a,b)=>{
return a-b;})===a.reverse()); // true
</script></body><html>
You can sort strings and objects too.
RESETRUNFULL
<!DOCTYPE html><html><body><script>
var nameList = [{name:'Mike',age:12,sex:'m'},{name:'Jane',age:11,sex:'f'},{name:'Alvin',age:13,sex:'m'}];function comparator(a,b){
return (a.name < b.name)?-1
:(a.name > b.name)?1:0}console.log(JSON.stringify(nameList.sort(comparator)));/*[{name:'Alvin',age:13,sex:'m'} {name:'Jane',age:11,sex:'f'}, {name:'Mike',age:12,sex:'m'}]*/
</script></body><html>
RESETRUNFULL
<!DOCTYPE html><html><body><script>
function FYshuffle(array) {
var m = array.length, t, i; // While there remain elements to shuffle…
while (m) { // Pick a remaining element…
i = Math.floor(Math.random() * m--); // And swap it with the current element.
t = array[m];
array[m] = array[i];
array[i] = t;
}
return array;}
</script></body><html>
<!DOCTYPE html><html><body><script>
someArray.sort(() => Math.random() * 2 - 1); // a compact shuffler
</script></body><html>