// You should assign arrays with const.
// This means that you can add values to the array, but you can't completely replace it.
// The const keyword in front of an object implies that there is an object,
// and you're working with references to alter it.
// It also (correctly) implies that you should not attempt to reassign references
// to this object.
// If you plan to reassign references to the object, then you use let.
const myHouse = ['living_room', 'bath_room', 'kitchen'];
myHouse.push('dishwasher');
const myHouse = ['living_room', 'bath_room', 'kitchen'];
myHouse = ['new_house']; // error
const array = [];
array.toString() // Returns a comma seperated list that is a string of all the values
array.join() // Similar to string method, but you can chose the seperator like
array.join(' and ')
let array3 = array.concat(array2) // joins array and array 2 together, can concat more
// than one array
const months = ['Jan', 'March', 'April', 'June'];
months.splice(1, 0, 'Feb');
// Inserts at index 1
console.log(months);
// Expected output: Array ["Jan", "Feb", "March", "April", "June"]
months.splice(4, 1, 'May');
// Replaces 1 element at index 4
console.log(months);
// Expected output: Array ["Jan", "Feb", "March", "April", "May"]
const myFish = ["angel", "clown", "trumpet", "sturgeon"];
const removed = myFish.splice(0, 2, "parrot", "anemone", "blue");
// myFish is ["parrot", "anemone", "blue", "trumpet", "sturgeon"]
// removed is ["angel", "clown"]
array.indexOf('joe'); // find position of that element, the first occurence
array.lastIndexOf('joe'); // find last position of that element
let nums = array.flat(); // nums will be array with one less layer of arrays
// but you can specify how many layers you want to flatten
array.push(...items) // Add items to the end of the array.
array.pop() // Extracts last item of the array
array.shift() // Extracts the first item of the array
array.unshift(...items) // Adds items to the beginning of the array
array.forEach() // perform a function to each element of the array
const array1 = [1, 4, 9, 16];
// Pass a function to map
const map1 = array1.map(x => x * 2);
console.log(map1);
// Expected output: Array [2, 8, 18, 32]
const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];
const result = words.filter(word => word.length > 6);
console.log(result);
// Expected output: Array ["exuberant", "destruction", "present"]
const numbers = [175, 50, 25];
document.getElementById("demo").innerHTML = numbers.reduce(myFunc);
function myFunc(total, num) {
return total - num;
}
const array1 = [1, 2, 3, 4];
// 0 + 1 + 2 + 3 + 4
const initialValue = 0;
const sumWithInitial = array1.reduce(
(accumulator, currentValue) => accumulator + currentValue,
initialValue
);
console.log(sumWithInitial);
// Expected output: 10
// can be useful to find the max value of the array
let num = [6, 4, 3, 8];
let someGreaterThan5 = nums.some((n) => n > 5);
// returns true because there exists some values greater than 5
let num = [6, 7, 9, 10]
let all GreaterThan5 = nums.every((n) => n > 5);
// returns true becasure every value is greater than 5
const inventory = [
{ name: "apples", quantity: 2 },
{ name: "bananas", quantity: 0 },
{ name: "cherries", quantity: 5 },
];
function isCherries(fruit) {
return fruit.name === "cherries";
}
console.log(inventory.find(isCherries));
// { name: 'cherries', quantity: 5 }
console.log(inventory.findIndex(isCherries))
// 2
const months = ['March', 'Jan', 'Feb', 'Dec'];
months.sort();
console.log(months);
// Expected output: Array ["Dec", "Feb", "Jan", "March"]
// to sort numbers:
const numericStringArray = ["80", "9", "700"];
numericStringArray.sort(compareNumbers); // ['9', '80', '700']

Note: do not use forEach for asynchronous code.
→ Array.prototype.forEach is not designed for asynchronous code. (It was not suitable for promises, and it is not suitable for async-await.)
Use a for...of construction instead!
for (const player of players) {
await givePrizeToPlayer(player);
}
⭐ https://gist.github.com/joeytwiddle/37d2085425c049629b80956d3c618971
Or you can use array.reduce() (this could be more performant)