array access first Element | const cars = ["Saab", "Volvo", "BMW"]; let car = cars[0];
| |
array access last Element | const fruits = ["Banana", "Orange", "Apple", "Mango"]; let fruit = fruits[fruits.length - 1];
| |
array add Element | const fruits = ["Banana", "Orange", "Apple"]; fruits.push("Lemon");
| -> last element |
array add Element | const fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits[fruits.length] = "Kiwi";
| |
array add Element push() | const fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.push("Kiwi");
| |
array add Element splice() | const fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.splice(2, 2, "Lemon", "Kiwi");
| [Banana,Orange,Lemon,Kiwi] |
array add Element splice() | const fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.splice(2, 0, "Lemon", "Kiwi");
| [items to add] [items to remove] |
array add first Elemenet unshift() | const fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.unshift("Lemon");
| |
array change Element | const fruits = ["Banana", "Orange", "Apple"]; fruits[6] = "Lemon";
| ⛔ Creates undefined "holes" in fruits |
array change Element | const cars = ["Saab", "Volvo", "BMW"]; cars[0] = "Opel";
| |
array compare function | function(a, b){return a - b}
| -1= a before b, 1 = b before a |
array compare function | const points = [40, 100, 1, 5, 25, 10]; document.getElementById("demo").innerHTML = points;
function myFunction1() { points.sort(); document.getElementById("demo").innerHTML = points; }
function myFunction2() { points.sort(function(a, b){return a - b}); document.getElementById("demo").innerHTML = points; }
| |
array concatenate concat() | const arr1 = ["Cecilie", "Lone"]; const arr2 = ["Emil", "Tobias", "Linus"]; const arr3 = ["Robin", "Morgan"]; const myChildren = arr1.concat(arr2, arr3);
| returns new array |
array concatenate concat() | const arr1 = ["Emil", "Tobias", "Linus"]; const myChildren = arr1.concat("Peter");
| returns new array |
array concatenate spread Operator | const q1 = ["Jan", "Feb", "Mar"]; const q2 = ["Apr", "May", "Jun"]; const q3 = ["Jul", "Aug", "Sep"]; const q4 = ["Oct", "Nov", "Dec"];
const year = [...q1, ...q2, ...q3, ...q4]
| |
array convert join() | const fruits = ["Banana", "Orange", "Apple", "Mango"]; document.getElementById("demo").innerHTML = fruits.join(" * ");
| Method, specify separator |
array convert String | const fruits = ["Banana", "Orange", "Apple", "Mango"]; document.getElementById("demo").innerHTML = fruits;
| equivalent fruits.toString() |
array convert toString() | const fruits = ["Banana", "Orange", "Apple", "Mango"]; document.getElementById("demo").innerHTML = fruits.toString();
| Method |
array create array from object | | |
array cut Elements slice() | const fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"]; const citrus = fruits.slice(2);
| [Lemon,Apple,Mango] |
array cut Elements slice() | const fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"]; const citrus = fruits.slice(1, 3);
| [start] [end excluded] [Orange,Lemon] |
array cut Elements slice() | const fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"]; const citrus = fruits.slice(1);
| creates new array |
array declaration | const points = new Array(40, 100, 1, 5, 25, 10);
| ⛔ avoid use new Constructor |
array declaration | var cars = ["Volvo", "BMW"]; const cars = ["Volvo", "BMW"]; { var cars = ["Volvo", "BMW"]; const cars = ["Volvo", "BMW"]; }
| ⛔ Reassignment const |
array declaration | const cars = ["Volvo", "BMW"]; const cars = ["Volvo", "BMW"]; var cars = ["Volvo", "BMW"]; cars = ["Volvo", "BMW"];
{ const cars = ["Volvo", "BMW"]; const cars = ["Volvo", "BMW"]; var cars = ["Volvo", "BMW"]; cars = ["Volvo", "BMW"]; }
| ⛔ Reassignment const same Scope |
array declaration const | const cars; cars = ["Saab", "Volvo", "BMW"];
| ⛔ const arrays must be initialized |
array declaration const | const cars = ["Volvo", "BMW"]; { const cars = ["Volvo", "BMW"]; } { const cars = ["Volvo", "BMW"]; }
| allowed = different Scope |
array declaration initialisation | const cars = []; cars[0]= "Saab"; cars[1]= "Volvo"; cars[2]= "BMW";
| |
array declaration reassignment const | const cars = ["Saab", "Volvo", "BMW"]; cars = ["Toyota", "Volvo", "Audi"];
| ⛔ Reassignment const |
array declaration reassignment var | var cars = ["Volvo", "BMW"]; var cars = ["Toyota", "BMW"]; cars = ["Volvo", "Saab"];
| allowed |
array declaration var | cars = ["Saab", "Volvo", "BMW"]; var cars;
| note VAR |
array delete Element delete() | const fruits = ["Banana", "Orange", "Apple", "Mango"]; delete fruits[0];
| ⛔ undefined holes, use pop() / shift() |
array delete first Elemenet shift() | const fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.shift();
| |
array delete last Elemenet pop() | const fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.pop();
| |
array dimensionality flat() | const myArr = [[1,2],[3,4],[5,6]]; const newArr = myArr.flat();
| |
array filter every() | const numbers = [45, 4, 9, 16, 25]; let allOver18 = numbers.every(myFunction);
function myFunction(value, index, array) { return value > 18; }
| |
array filter every() | const numbers = [45, 4, 9, 16, 25]; let allOver18 = numbers.every(myFunction);
function myFunction(value) { return value > 18; }
| |
array filter find() | const numbers = [4, 9, 16, 25, 29]; let first = numbers.find(myFunction);
function myFunction(value, index, array) { return value > 18; }
| returns only first occurrence |
array filter findIndex() | const numbers = [4, 9, 16, 25, 29]; let first = numbers.findIndex(myFunction);
function myFunction(value, index, array) { return value > 18; }
| returns only first occurrence |
array filter includes() | const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.includes("Mango");
| including NaN |
array filter indexOf() | const fruits = ["Apple", "Orange", "Apple", "Mango"]; let position = fruits.indexOf("Apple") + 1;
| returns only first occurrence |
array filter lastIndexOf() | const fruits = ["Apple", "Orange", "Apple", "Mango"]; let position = fruits.lastIndexOf("Apple") + 1;
| returns only last occurrence |
array filter some() | const numbers = [45, 4, 9, 16, 25]; let someOver18 = numbers.some(myFunction);
function myFunction(value, index, array) { return value > 18; }
| |
array filter() | const numbers = [45, 4, 9, 16, 25]; const over18 = numbers.filter(myFunction);
function myFunction(value) { return value > 18; }
| |
array filter() | const numbers = [45, 4, 9, 16, 25]; const over18 = numbers.filter(myFunction);
function myFunction(value, index, array) { return value > 18; }
| creates new array |
array find highest custom function | function myArrayMax(arr) { let len = arr.length; let max = -Infinity; while (len--) { if (arr[len] > max) { max = arr[len]; } } return max; }
| best practice |
array find highest lowest Element asc | const points = [40, 100, 1, 5, 25, 10]; points.sort(function(a, b){return a - b});
| no existing built-in function |
array find highest lowest Element desc | const points = [40, 100, 1, 5, 25, 10]; points.sort(function(a, b){return b - a});
| inefficient method |
array find highest Math.max() | function myArrayMax(arr) { return Math.max.apply(null, arr); }
| equivalent Math.max(1, 2, 3) |
array find lowest custom function | function myArrayMin(arr) { let len = arr.length; let min = Infinity; while (len--) { if (arr[len] < min) { min = arr[len]; } } return min; }
| best practice |
array find lowest Math.min() | function myArrayMin(arr) { return Math.min.apply(null, arr); }
| equivalent Math.min(1, 2, 3) |
array iterate | const cars = ["Saab", "Volvo", "BMW"]; document.getElementById("demo").innerHTML = cars;
| |
array iterate entries() | const fruits = ["Banana", "Orange", "Apple", "Mango"]; const f = fruits.entries();
for (let x of f) { document.getElementById("demo").innerHTML += x; }
| return key value pairs |
array iterate for() | const fruits = ["Banana", "Orange", "Apple", "Mango"]; let fLen = fruits.length;
let text = "<ul>"; for (let i = 0; i < fLen; i++) { text += "<li>" + fruits[i] + "</li>"; } text += "</ul>";
| |
array iterate forEach() | const numbers = [45, 4, 9, 16, 25]; let txt = ""; numbers.forEach(myFunction);
function myFunction(value, index, array) { txt += value + "<br>"; }
| callback function |
array iterate forEach() | const numbers = [45, 4, 9, 16, 25]; let txt = ""; numbers.forEach(myFunction);
function myFunction(value) { txt += value + "<br>"; }
| |
array iterate keys() | const fruits = ["Banana", "Orange", "Apple", "Mango"]; const keys = fruits.keys();
for (let x of keys) { text += x + "<br>"; }
| returns index numbers |
array Property instanceof | const fruits = ["Banana", "Orange", "Apple"];
fruits instanceof Array;
| returns true if created by given constructor |
array Property isArray() | | |
array Property length | const fruits = ["Banana", "Orange", "Apple", "Mango"]; let length = fruits.length;
| |
array Property typeof | const fruits = ["Banana", "Orange", "Apple"]; let type = typeof fruits;
| ⛔ returns: is object |
array run function dimension flatMap() | const myArr = [1, 2, 3, 4, 5, 6]; const newArr = myArr.flatMap((x) => x * 2);
| |
array run function map() | const numbers1 = [45, 4, 9, 16, 25]; const numbers2 = numbers1.map(myFunction);
function myFunction(value, index, array) { return value * 2; }
| creates new array |
array run function map() | const numbers1 = [45, 4, 9, 16, 25]; const numbers2 = numbers1.map(myFunction);
function myFunction(value) { return value * 2; }
| no Execution for array without values |
array run function reduce() | const numbers = [45, 4, 9, 16, 25]; let sum = numbers.reduce(myFunction);
function myFunction(total, value) { return total + value; }
| |
array run function reduce() | const numbers = [45, 4, 9, 16, 25]; let sum = numbers.reduce(myFunction);
function myFunction(total, value, index, array) { return total + value; }
| returns single value, from [0] -> [end] |
array run function reduceRight() | const numbers = [45, 4, 9, 16, 25]; let sum = numbers.reduceRight(myFunction);
function myFunction(total, value) { return total + value; }
| |
array run function reduceRight() | const numbers = [45, 4, 9, 16, 25]; let sum = numbers.reduceRight(myFunction);
function myFunction(total, value, index, array) { return total + value; }
| returns single value, from [end] -> [0] |
array scope | const cars = ["Saab", "Volvo", "BMW"];
{ const cars = ["Toyota", "Volvo", "BMW"]; }
| const=blockScope, var=globalScope |
array sort asc sort() | const fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.sort();
| alphabetically ascending |
array sort desc reverse() | const fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.sort(); fruits.reverse();
| alphabetically descending |
array sort numeric asc | const points = [40, 100, 1, 5, 25, 10]; points.sort(function(a, b){return a - b});
| sort() produces incorrect result |
array sort numeric desc | const points = [40, 100, 1, 5, 25, 10]; points.sort(function(a, b){return b - a});
| sort() produces incorrect result |
array sort random order function | const points = [40, 100, 1, 5, 25, 10];
for (let i = points.length -1; i > 0; i--) { let j = Math.floor(Math.random() * (i+1)); let k = points[i]; points[i] = points[j]; points[j] = k; }
| Fisher Yates Method |
array sort random order function | const points = [40, 100, 1, 5, 25, 10]; points.sort(function(){return 0.5 - Math.random()});
| not accurate |
array sorting object function | const cars = [ {type:"Volvo", year:2016}, {type:"Saab", year:2001}, {type:"BMW", year:2010} ]; cars.sort(function(a, b){ let x = a.type.toLowerCase(); let y = b.type.toLowerCase(); if (x < y) {return -1;} if (x > y) {return 1;} return 0; });
| |