Learning JavaScript
Arrays and Objects
Syntax for creating an array
//using new keyword with Array() constructor
const someArray = new Array(length);
//using static Array.of() method
const someArray = Array.of();
//using an array literal
const arrayName = [];
Create an array then assign values
const someArray = new Array(3);
someArray[0] = "house";
someArray[1] = "garage";
someArray[2] = "shed";
Create array and assign values at same time
//using new keyword with Array() constructor
const someArray = new Array(arrayList)
//example
const loanRates = new Array(6.8, 12.8, 18.65);
//using static Array.of() method
const someArray = Array.of(arrayList);
//example
const daysOfWeek = Array.of(1,2,3,4,5,6,7);
//using an array literal
const someArray = [arrayList];
//example
const names = ["Tom", "Duke", "Charlie"];
Referring to element in an array
nameOfArray[index];
//arrays are 0 indexed. So 0 will refer to first element in array, 1 will refer to the second and so on
//example from names array above
console.log(names[1]); // output "Duke"
Get properties of an array
length //how many elements/cells an array has
//example from names array above
console.log(names.length);
Add elements to end of an array
const numbers = [4,5,6]; //array is 4,5,6
numbers[numbers.length] = 7; //array is now 4,5,6,7
//using push method
numbers.push(8); //array is now 4,5,6,7,8
Add elements to beginning of an array
const numbers = [4,5,6]; //array is 4,5,6
numbers.unshift(3); //array is now 3,4,5,6
Add elements to specific index
const numbers = [4,5,6]; //array is 4,5,6
numbers[5] = 11; //array is now 4,5,6,undefined,undefined,11
Loop through array
const names = ["Tom", "Duke", "Charlie"];
//regular for loop
for(let i = 0; i < names.length; i++) {
console.log(names[i]);
}
//output
// Tom
// Duke
// Charlie
//for-in loop
for(const|let index in arrayName) {
...
}
//example
for(let index in names) {
console.log(names[index]);
}
//output
// Tom
// Duke
// Charlie
//for-of loop
for(const value of arrayName) {
... //bonus: you don't even have to use the index
}
//example
for(const value of names) {
console.log(value);
}
//output
// Tom
// Duke
// Charlie
Remove element(s) from an array
//remove one element
const numbers = [4,5,6]; //array is 4,5,6
delete numbers[1]; //array is now 4,undefined,6
//remove all elements
numbers.length = 0;
//remove element from beginning of array
const names = ["Tom", "Duke", "Charlie"];
names.shift(); //array is now Duke, Charlie
Destructure an array
const numbers = [4,5,6]; //array is 4,5,6
//example
const[first, second, third] = numbers;
//first is 4, second is 5, third is 6
//example using a string
const[letter1, letter2, letter3] = "USA";
//letter1 is U, letter2 is S, letter3 is A
Find specific value in array
const cars = ["volvo", "chevy", "chrysler","mercedes"];
if(cars.indexOf("chrysler") != -1 ) { ... } //-1 is returned if doesn't exist
if(cars.includes("checy")) { ... }
Filtering array
//example- get all numbers from array less than 10
const array = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20];
//using lambda, read as "const less ten equals array dot filter value such that value is less than 10"
const lessTen = array.filter( value => value < 10);
Convert elements in array to single string
const names = ["Sally", "Anne", "Struthers"];
let fullName = names.join(); //fullName is "Sally,Anne,Struthers"
let fullName = names.join(", "); //fullName is "Sally, Anne, Struthers"
let fullName = names.join(""); //fullName is "SallyAnneStruthers"
let fullName = names.toString(); //"Sally,Anne,Struthers"
Sort numeric values in ascending order
const numbers = [1,14,7,6,27,3];
numbers.sort((x,y) => x-y); //numbers is now [ 1, 3, 6, 7, 14, 27 ]
Split a string separated by spaces into array
const fullName = "Sally Anne Struthers";
const separateName = fullName.split(" "); //array ["Sally", "Anne", "Struthers"]
Save array object into JSON
const chores = [];
chores.push(["sweep", new Date("02/17/2022")]);
chores.push(["dishes", new Date("02/18/2022")])
//convert array to JSON
const json = JSON.stringify(chores);
//save to storage
localStorage.chores = json;
Split a string separated by spaces into array
const fullName = "Sally Anne Struthers";
const separateName = fullName.split(" "); //array ["Sally", "Anne", "Struthers"]
Creating and Accessing maps
const mapName = new Map([arrayOfArrays]);
//create empty map
const map = new Map();
//create a map from an array of arrays
const names = new Map([ ["Galileio", "Galilei"], ["Rosalind","Franklin"] ]);
//access the map
if(names.has("Rosalind")) {
console.log(names.get("Rosalind")); //returns "Franklin"
}
Object literals (object is just an entity with "sub" parts)
const invoice = { //not a class
taxes: 0.24, //property
getTotal(subtotal) { //method
const salesTax = subtotal * this.taxes;
return subtotal + salesTax; //this = the object
}
};
//access object with dot notation
console.log(invoice.taxes); //output 0.24
const amount = invoice.getTotal(100); //returns 124