// Javascript Array Methods
[3, 4, 5, 6].at(1); // 4
[3, 4, 5, 6].pop(); // [3, 4, 5]
[3, 4, 5, 6].push(7); // [3, 4, 5, 6, 7]
[3, 4, 5, 6].fill(1); // [1, 1, 1, 1]
[3, 4, 5, 6].join("-"); // 3-4-5-6
[3, 4, 5, 6].shift(); // [4, 5, 6]
[3, 4, 5, 6].reverse(); // [6, 5, 4, 3]
[3, 4, 5, 6].unshift(1); // [1, 3, 4, 5, 6]
[3, 4, 5, 6].includes(5); // true
[3, 4, 5, 6].map((num) => num + 6); // [9, 10, 11, 12]
[3, 4, 5, 6].find((num) => num > 4); // 5
[3, 4, 5, 6].filter((num) => num > 4); // [5, 6]
[3, 4, 5, 6].every((num) => num > 5); // false
[3, 4, 5, 6].findeIndex((num) => num > 4); // 2
[3, 4, 5, 6].reduce((acc, num) => acc + num, 0); // 18
dogs.toString(); // convert to string: results "Bulldog,Beagle,Labrador"
dogs.join(" * "); // join: "Bulldog * Beagle * Labrador"
dogs.pop(); // remove last element
dogs.push("Chihuahua"); // add new element to the end
dogs[dogs.length] = "Chihuahua"; // the same as push
dogs.shift(); // remove first element
dogs.unshift("Chihuahua"); // add new element to the beginning
delete dogs[0]; // change element to undefined (not recommended)
dogs.splice(2, 0, "Pug", "Boxer"); // add elements (where, how many to remove, element list)
var animals = dogs.concat(cats,birds); // join two arrays (dogs followed by cats and birds)
dogs.slice(1,4); // elements from [1] to [4-1]
dogs.sort(); // sort string alphabetically
dogs.reverse(); // sort string in descending order
x.sort(function(a, b){return a - b}); // numeric sort
x.sort(function(a, b){return b - a}); // numeric descending sort
highest = x[0]; // first item in sorted array is the lowest (or highest) value
x.sort(function(a, b){return 0.5 - Math.random()}); // random order sort
// STRING
// string[index] - get a certain character of a string
// string.length - return the number of characters in a string
// string.split(' ') - returns an array of words of a string
// string.split('') - returns an array of characters of a string
// string.toLowerCase() - returns a lowercased string
// string.toUpperCase() - returns an uppercased string
// string.includes('subtring') - checks whether a substring exists inside of a string [check the characther case]
// ARRAY
// array[index] - returns a certain value from an array
// array.indexOf('value') - returns the index of the first occurance of that value
// array.length - returns the number of elements in the array
// array.join('') - returns a string of array values
// array.push(value) - adds the value to the end of the array
// array.pop() - removes the value from the end of the array
// array.unshift(value) - adds the value to the start of the array
// array.shift() - removes the value from the start of the array
// array.splice(fromIndex, number_of_elements) - removes the number_of_elements, starting from fromIndex from the array
// array.slice(fromIndex, toIndex) - copies a certain part of the array
// for - looping
const emojis = [ '😀', '😆', '🙃', 'ðŸ˜' ];
const wavingEmojis = [];
for (let i = 0; i < emojis.length; i++) {
wavingEmojis.push(`👋${emojis[i]}👋`);
}
// forEach - array method for looping
emojis.forEach((emoji) => console.log(emoji));
// map - array method for looping BUT IT HAS RETURNS
const wavingEmojis = emojis.map((emoji) => `👋${emoji}👋`);
// filter
const numbers = [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ];
const numbersBiggerThanFive = numbers.filter((number) => number > 5);
// sort
const numbers = [ 3, 4, 1, 5, 4, 7, 2, 23, 12 ];
const sortFromSmalles = numbers.sort((a, b) => a - b);
const sortFromLargest = numbers.sort((a, b) => b - a);
// VALUE VS REFERENCE (part 1: intro)
// arrays
const numbers = [ 1, 2, 3, 4 ]; // #123asd
const anotherNumbers = numbers; // #123asd
anotherNumbers.push(5);
// objects
const person = {
firstName: 'John',
lastName: 'Doe'
};
const anotherPerson = person;
anotherPerson.lastName = 'DOEEEE';
console.log(numbers === anotherNumbers); // true
console.log(person === anotherPerson) // true
// VALUE VS REFERENCE (part 2: CLONING ARRAYS AND OBJECTS)
// SHALLOW CLONING - ONE LEVEL DEEP
const original = [ 1, 2, 3 ];
const newOriginal = [...original];
// DEEP CLONING - TWO LEVELS DEEP
const users = [ { name: 'John', age: 25 }, { name: 'Victor', age: 25 }, { name: 'Adrian', age: 25 } ];
const newUsers = JSON.parse(JSON.stringify(users));