Arrays is used to store a list of items, items could be an array(list) of themselves and thereby quite powerful. Arrays is actual objects in JS but a special object with built-in functions on the array prototype.
I will go through some of the most common and useful functions here:
.length checks the length of the array.
var arr = [1,4,67,"dd"]; console.log(arr.length); // -> 4
.join() flattens the array into a string with or without a seperator.
var arr = [1,4,67,"dd"]; console.log(arr.join('')); // -> "1467dd"
.split() do the opposite and put a string into an array, the parameter is where it should be splitting, if nothing is passed in it will split after each char
var str = "Hello world"; var arr1 = str.split(""); var arr2 = str.split(" "); console.log(arr1); // -> ["H", "e", "l", "l", "o", " ", "w", "o", "r", "l", "d"] console.log(arr2); // -> ["Hello", "world"]
.push() which adds an item to the end of the array
var arr = [1,4,67,"dd"]; arr.push("wopp"); console.log(arr[4]); // -> "wopp"
You might be confused how the length could have been 4 before and now when we take the 5th element we get the new value by targeting the index of 4.
The reason is that .length counts from 1. Therefor 4 elements becomes 4, 1,2,3,4.
While the index numbers are numbered from 0. So an array with the indexes of 0,1,2,3 have the length of 4.
.shift() takes the first item away, optionally stored in a variable if needed
var arr = [1,4,67,"dd"]; console.log(arr[0]); // -> 1 var firstItem = arr.shift()//removes the first item, 1 and places it an variable called firstItem console.log(arr[0]); // -> 4 console.log(firstItem); //->1
.unshift() adds an item to the start of an array
var arr = [1,4,67,"dd"]; console.log(arr[0]); // -> 1 arr.unshift("new"); console.log(arr[0]); // -> "new"
.pop() takes the last item away, optionally stored in a variable if needed
var arr = [1,4,67,"dd"]; console.log(arr[3]); // -> "dd" var lastItem = arr.pop()//removes the first item, 1 and places it an variable called firstItem console.log(arr[3]); // -> undefined console.log(lastItem); //->"dd"
.reverse() for reversing the order of the items
var arr = [1,4,67,"dd"]; console.log(arr[0]); //-> 1 arr.reverse() console.log(arr[0]); // -> "dd"
.indexOf() finds the index of an item so it becomes very easy to search up the index of a specific value in the array
var arr = [1,4,67,"dd"]; console.log(arr.indexOf("dd"));// -> 3
.slice(); returns part of an array or if no arguments been passed the whole array, very handy when you wanna make a copy of an array. slice doesn’t leave any marks on the original array so it’s almost always used with a variable declaration.
var arr = [1,4,67,"dd"]; var copiedArr = arr.slice(); //creates a duplicate without any strings attached to the original array var slicedArr = copiedArr.slice(0,2);//starting from index 0 and goes up to all indexes less than 2. slicedArr >= startingvalue && slicedArr < endvalue console.log(copiedArr);//1,4,67,"dd" console.log(slicedArr);//1,4
.sort() very useful to sort an array
var arr = [1, 2, 100, 12, -1]; // sort in ascending order arr.sort(function(a, b) { return a - b; }); // [-1, 1, 2, 12, 100] // sort in descending order arr.sort(function(a, b) { return b - a; }); // [100, 12, 2, 1, -1]
.map() very useful to do logic on or concatenate strings with and then just add that to the array
const inventors = [ { first: 'Albert', last: 'Einstein', year: 1879, passed: 1955 }, { first: 'Isaac', last: 'Newton', year: 1643, passed: 1727 }, { first: 'Galileo', last: 'Galilei', year: 1564, passed: 1642 }, { first: 'Marie', last: 'Curie', year: 1867, passed: 1934 }, { first: 'Johannes', last: 'Kepler', year: 1571, passed: 1630 }, { first: 'Nicolaus', last: 'Copernicus', year: 1858, passed: 1543 }, { first: 'Max', last: 'Planck', year: 1858, passed: 1947 }, { first: 'Katherine', last: 'Blodgett', year: 1898, passed: 1979 }, { first: 'Ada', last: 'Lovelace', year: 1815, passed: 1852 }, { first: 'Sarah E.', last: 'Goode', year: 1829, passed: 1905 }, { first: 'Lise', last: 'Meitner', year: 1829, passed: 1968 }, { first: 'Hanna', last: 'Hammarström', year: 1829, passed: 1909 } ]; const fullNames = inventors.map(inventor => inventor.first + ' ' + inventor.last); console.log(fullNames);
.reduce() very useful when adding up a total set of numbers from the values in the array
const inventors = [ { first: 'Albert', last: 'Einstein', year: 1879, passed: 1955 }, { first: 'Isaac', last: 'Newton', year: 1643, passed: 1727 }, { first: 'Galileo', last: 'Galilei', year: 1564, passed: 1642 }, { first: 'Marie', last: 'Curie', year: 1867, passed: 1934 }, { first: 'Johannes', last: 'Kepler', year: 1571, passed: 1630 }, { first: 'Nicolaus', last: 'Copernicus', year: 1858, passed: 1543 }, { first: 'Max', last: 'Planck', year: 1858, passed: 1947 }, { first: 'Katherine', last: 'Blodgett', year: 1898, passed: 1979 }, { first: 'Ada', last: 'Lovelace', year: 1815, passed: 1852 }, { first: 'Sarah E.', last: 'Goode', year: 1829, passed: 1905 }, { first: 'Lise', last: 'Meitner', year: 1829, passed: 1968 }, { first: 'Hanna', last: 'Hammarström', year: 1829, passed: 1909 } ]; //total is the one that is saving the value through each iteration const totalYear = inventors.reduce((total, inventor) => { return total + (inventor.passed - inventor.year); }, 0); //Years all the inventors lived console.log(totalYear);