Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save imtumbleweed/1631463af601601da94805cfe0fe0e2f to your computer and use it in GitHub Desktop.
Save imtumbleweed/1631463af601601da94805cfe0fe0e2f to your computer and use it in GitHub Desktop.
JavaScript Array postmortem
// Convert array to string
[1,2,3,"hello","there",4,5].toString(); // 1,2,3,hello,there,4,5
// Return the array itself
[1,2,3].valueOf(); // [1, 2, 3]
// Check if an object or a primitive is an Array
Array.isArray([1]); // true
Array.isArray( 1 ); // false
// Pushing
var e = [0, 1, 2];
e.pop(); // 2
e; // [0, 1]
// Popping
var f = [0, 1, 2];
f.push(3); // 4
f; // [0, 1, 2, 3]
var g = [0, 1, 2];
// Remove first value and returns that value
g.shift(); // 0; after this g is [1, 2]
// Add an item to the beginning of an array, and return its length
g.unshift("a"); // 3; after this g is ["a", 1, 2]
// Creates 5 numeric entries
var A = new Array(1, 2, 3, 4, 5);
A; // 1,2,3,4,5
// Creates 5 empty entries when one numeric argument is passed
var B = new Array(5);
B; // [undefined × 5] or ",,,,"
var C = new Array("A", "B", "C");
C; // ["A", "B", "C"]
var D = new Array("D", "E", "F");
D; // ["D", "E", "F"]
var E = new Array(0, 0, 0, 1, 1, 1);
E; // [0, 0, 0, 1, 1, 1]
A.constructor); // function Array() { [native code] }
A.length); // 5
A.prototype); // undefined
A.__proto__); // [constructor: function, toString: function, toLocaleString: function, join: function, pop: function…]
// Concatenate array C to the end of array A
A = A.concat(C); // [1, 2, 3, 4, 5, "A", "B", "C"]
// Concatenate array C and D to the end of array A
A = A.concat(C, D); // [1, 2, 3, 4, 5, "A", "B", "C", "D", "E", "F"]
// copyWithin(target, - required
// start, - required
// end); - optional
// Copy everything in array A from index "0" to length, to index "1" (shifting it forward)
A.copyWithin(1, 0); // [1, 1, 2, 3, 4, 5, "A", "B", "C", "A", "B", "C", "D", "E"]
// Copy everything in array C from index "1" (which evaluates to ["B", "C"]) to index "0"
C.copyWithin(0, 1); // ["B", "C", "C"]
// Copy everything in array E from index "3" (which evaluates to [1,1,1]) to index 0,
// but copy only up to index "5" in the entire array E
// when skipped, the default value for argument 3 is "length" of the array
E.copyWithin(0, 3, 5); // [1,1,0,1,1,1]
// A bewildering JavaScript array example
[0,1,2,3,4,5].every( function(n) { n); return n > 1} ); // returns "false"
// Filter method
[0,1,2,3,4,5].filter((n) =>{ return n > 1}); // returns [2,3,4,5]
// Fill first 3 entries with "a"
[0,1,2,3,4,5].fill("a", 0, 3); // ["a", "a", "a", 3, 4, 5]
// Fill all with "a"
[0,1,2,3,4,5].fill("a"); // ["a", "a", "a", "a", "a", "a"]
// Return the index of first element greater or equal to 1
[0,1,2,3,4,5].findIndex((n) => {return n >= 1;}); // 1
// Walk each index and value of the array
[0,1,2].forEach((index, value) => { "index = " + index + " = " + value) });
// Find index of an item by value
["a","b"].indexOf("b"); // 1
// Find last index of (for repeat items)
["a","a","a"].lastIndexOf("a"); // 2
// Create a string of array values with a separator character
["a","b"].join("+"); // "a+b"
// Return result of a function on array entries without changing original array
[4,16,64].map(Math.sqrt); // [2, 4, 8]
// Array.reduce(function(total, currentvalue, currentindex, array), initialvalue)
// Example 1.
// In a reducer, the return value is stored in an accumulator
[1,2,3].reduce(function(a, b, i, arr){ arguments); return a + b; });
// Results in two iterations logged to console
// [1, 2, 1, Array(3), ...]
// [3, 3, 2, Array(3), ...]
// Followed by final return value "6" (the sum of array items)
// 6
// Example 2; concatenate multiple arrays
[[0,1],[2,3]].reduce(function(a, b, i, arr) { return a.concat(b); });
// [0, 1, 2, 3]
// Example 3; reduceRight: concatenate multiple arrays
[[3,4],[1,2]].reduceRight(function(a, b, i, arr) { return a.concat(b); });
// [1, 2, 3, 4]
[0,1,2,3].reverse(); // [3, 2, 1, 0]
// Select and return a span of an array
[0,1,2,3,4,5].slice(2,4); // [2,3]
// splice: splice(index,howmany, item1,item2,item3...,itemx)
// return: added or removed items
// Splice to insert
var a = [1, 3];
a.splice(2, 0, "a", "b"); // [3] (return value)
a; // [1, 3, "a", "b"] (resulting array)
// Splice to remove ranges
[1,2,3].splice(); // [] (return value; nothing was removed)
[1,2,3].splice(0); // [1,2,3] (return value; removed all values)
[1,2,3].splice(0,1); // [1] (return value; removed first item)
[1,2,3].splice(0,2); // [1, 2] (return value; removed first two items)
[1,2,3].splice(2); // [3] (return value; removed last item)
// Check if at least one element in array matches a condition
[1,2,3,4,5].some(function(a) { return a == 3; } );
// true
// Array-sorting functions
["c","b","a"].sort(); // ["a", "b", "c"]
["3","2","1"].sort(); // ["1", "2", "3"]
[3,2,1].sort(); // [1, 2, 3]
// Supply your own sorting algorithm
[1,3,2].sort(function(a,b){return a-b;}); // [1, 2, 3]
[1,3,2].sort(function(a,b){return b-a;}); // [3, 2, 1]
// Reverse array
[1,2,3].reverse(); // [3, 2, 1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment