Created
March 21, 2019 13:50
-
-
Save Xotabu4/444bfa2da1c4372fb301aac79236f3a5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Arrays | |
let arr = new Array(); | |
let arr = []; // mostly used | |
// Declare array with some initial data: | |
let fruits = ["Apple", "Orange", "Plum"]; | |
// GET element from array - use index | |
let fruits = ["Apple", "Orange", "Plum"]; | |
let apple = fruits[0]; // Apple | |
let orange = fruits[1]; // Orange | |
let plum = fruits[2]; // Plum | |
// Index is a number starts from 0 | |
// --0-- --1-- --2-- | |
// ["Apple", "Orange", "Plum"] | |
// UPDATE elements in array - use index | |
fruits[2] = "Pear"; // now ["Apple", "Orange", "Pear"] | |
// ADD new element to array - use index | |
fruits[3] = "Lemon"; // now ["Apple", "Orange", "Pear", "Lemon"] | |
// Arrays can contain ANY types inside, even mix of many types: | |
let arr = [ | |
"Apple", | |
{ name: "John" }, | |
true, | |
function() { | |
alert("hello"); | |
} | |
]; | |
// get the object at index 1 and then show its name | |
let n = arr[1]; // John | |
// get the function at index 3 and run it | |
arr[3](); // hello | |
// use isArray to check if your variable is array: | |
let arr = [1, 2, 3, 4, 5]; | |
console.log(Array.isArray(arr)); // true | |
// *************************************************************************** | |
// pop/push, shift/unshift | |
// pop - take away last element | |
let fruits = ["Apple", "Orange", "Pear"]; | |
console.log(fruits.pop()); // remove "Pear" and print it | |
console.log(fruits); // Apple, Orange | |
// push - add element to the end of array | |
let fruits = ["Apple", "Orange"]; | |
fruits.push("Pear"); | |
console.log(fruits); // Apple, Orange, Pear | |
// shift - take away first element | |
let fruits = ["Apple", "Orange", "Pear"]; | |
console.log(fruits.shift()); // remove Apple and print it | |
console.log(fruits); // Orange, Pear | |
// unshift - add element to begining | |
let fruits = ["Orange", "Pear"]; | |
fruits.unshift("Apple"); | |
console.log(fruits); // Apple, Orange, Pear | |
// You can add multiple elements at once | |
let fruits = ["Apple"]; | |
fruits.push("Orange", "Peach"); | |
fruits.unshift("Pineapple", "Lemon"); | |
// ["Pineapple", "Lemon", "Apple", "Orange", "Peach"] | |
console.log(fruits); | |
// *************************************************************************** | |
// Arrays are Objects: | |
let fruits = []; // make an array | |
fruits[100] = 5; // assign a property with the index far greater than its length | |
fruits.age = 25; // create a property with an arbitrary name | |
// Special property - length | |
// Represents number of items in array: | |
let fruits = []; | |
console.log(fruits.length); // 0 | |
fruits.push("a", "b", "c"); | |
console.log(fruits.length); // 3 | |
// Do not be confused with length\index! | |
console.log(fruits[2]); // "c" | |
// UNSAFE! | |
// You can modify value of length: | |
let fruits = []; | |
console.log(fruits.length); // 0 | |
fruits.push("a", "b", "c"); | |
console.log(fruits.length); // 3 | |
fruits.length = 1000; | |
console.log(fruits.length); // 1000 | |
fruits.push("d"); | |
console.log(fruits.length); // 1001 | |
// *************************************************************************** | |
// Iterating (loops) for array | |
let fruits = ["Apple", "Orange", "Pear"]; | |
// (1) Just regular 'for' loop with checking length | |
for (let i = 0; i < fruits.length; i++) { | |
console.log(fruits[i]); | |
} | |
// (2) for/of - special form for iterables(arrays) | |
let fruits = ["Apple", "Orange", "Plum"]; | |
// for..of doesn’t give access to the index of the current element | |
for (let fruit of fruits) { | |
console.log(fruit); | |
} | |
// (!) Do NOT use for/in loop (as for iterating over object properties) | |
let arr = ["Apple", "Orange", "Pear"]; | |
for (let key in arr) { | |
console.log(arr[key]); // Apple, Orange, Pear | |
} | |
// because for..in iterates over ALL properties, not just indexes | |
arr["some"] = "oops!"; | |
for (let key in arr) { | |
console.log(arr[key]); // Apple, Orange, Pear, oops! | |
} | |
// (3) forEach method, runs provided function for every member of array | |
let arr = ["Apple", "Orange", "Pear"]; | |
arr.forEach(function(item, index, array) { | |
console.log(item); | |
}); | |
// The result of the function (if it returns any) is thrown away and ignored. | |
let res = arr.forEach(function(item, index, array) { | |
return "NEW" + item; | |
}); | |
console.log(arr) | |
console.log(res) | |
// (4) .map() method | |
let arr = [10, 30, 100]; | |
let newArr = arr.map(function(item, index, array) { | |
console.log(item); | |
return item + 10; | |
}); | |
console.log(newArr); | |
// The result of the function will be passed to new array |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// *************************************************************************** | |
// Other array methods | |
// *************************************************************************** | |
// splice | |
// arr.splice(index[, deleteCount, elem1, ..., elemN]) | |
// starts from the position index: | |
// removes deleteCount elements | |
// and then inserts elem1, ..., elemN at their place. | |
// Returns the array of removed elements. | |
let arr = ["I", "study", "JavaScript"]; | |
arr.splice(1, 1); // from index 1 remove 1 element | |
console.log(arr); // ["I", "JavaScript"] | |
// ** | |
let arr = ["I", "study", "JavaScript", "right", "now"]; | |
// remove 3 first elements and replace them with another | |
arr.splice(0, 3, "Let's", "dance"); | |
console.log(arr); // now ["Let's", "dance", "right", "now"] | |
// ** | |
let arr = ["I", "study", "JavaScript", "right", "now"]; | |
// remove 2 first elements | |
let removed = arr.splice(0, 2); | |
console.log(removed); // "I", "study" | |
// ** | |
let arr = ["I", "study", "JavaScript"]; | |
// from index 2 | |
// delete 0 elements | |
// then insert "complex" and "language" | |
arr.splice(2, 0, "complex", "language"); | |
console.log(arr); // "I", "study", "complex", "language", "JavaScript" | |
// *************************************************************************** | |
// slice | |
// Returns a new array containing all items from index "start" to "end" | |
let arr = ["a", "b", "c", "d"]; | |
console.log(arr.slice(1, 3)); // [b,c] | |
console.log(arr.slice(-2)); // [c,d] | |
console.log(arr); | |
// *************************************************************************** | |
// concat | |
// Merges 2 arrays | |
let arr = [1, 2]; | |
// merge arr with [3,4] | |
console.log(arr.concat([3, 4])); // 1,2,3,4 | |
// merge arr with [3,4] and [5,6] | |
console.log(arr.concat([3, 4], [5, 6])); // 1,2,3,4,5,6 | |
// merge arr with [3,4], then add values 5 and 6 | |
console.log(arr.concat([3, 4], 5, 6)); // 1,2,3,4,5,6 | |
// *************************************************************************** | |
// sort | |
let arr = [1, 2, 15]; | |
// the method reorders the content of arr (and returns it) | |
arr.sort(); | |
console.log(arr); // 1, 15, 2 | |
// UNSAFE! | |
// Why it is sorted to - 1, 15, 2 ?????? | |
// The items are sorted as strings by default. | |
console.log("2" > "15"); // true | |
// To avoid this - supply your own sorting function: | |
function compareNumeric(a, b) { | |
if (a > b) return 1; | |
if (a == b) return 0; | |
if (a < b) return -1; | |
} | |
let arr = [1, 2, 15]; | |
arr.sort(compareNumeric); | |
console.log(arr); // 1, 2, 15 | |
// comparison function should return a positive number to say “greater” | |
// and a negative number to say “less”. | |
// 0 - "same" | |
// So you can write the same: | |
arr.sort((a, b) => a - b); | |
// *************************************************************************** | |
// reverse | |
let arr = [1, 2, 3, 4, 5]; | |
arr.reverse(); | |
console.log(arr); // 5,4,3,2,1 | |
// *************************************************************************** | |
// split and join | |
// split can be used to split string to array: | |
let names = 'one, two, three'; | |
let arr = names.split(', '); | |
console.log(arr) // [ 'one' 'two' 'three' ] | |
// join - will join array items to string: | |
let str = arr.join(';'); | |
console.log(str) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// searching | |
let fruits = ["Apple", "Orange", "Plum"]; | |
// looks for item starting from index from, and returns the index where it was found, otherwise -1. | |
let index = fruits.indexOf("Plum", 1); | |
// same, but looks from right to left. | |
let index = fruits.lastIndexOf("Plum"); | |
// includes returns true if found. | |
let isExist = fruits.includes("Orange"); | |
console.log(isExist); // true | |
// Note that the methods use === comparison | |
// *************************************************************************** | |
// find and findIndex | |
// Returns first item for which function returned 'true', or undefined | |
let users = [ | |
{ id: 1, name: "John", age: 21 }, | |
{ id: 2, name: "Pete", age: 43 }, | |
{ id: 3, name: "Mary", age: 37 } | |
]; | |
let user = users.find(function(item) { | |
return item.id == 1; | |
}); | |
console.log(user.name); // John | |
// findIndex works the same but returns index of element, or -1 if not found | |
let ind = users.findIndex(function(item) { | |
return item.id == 2; | |
}); | |
console.log(ind); // 1 | |
// some - to check if atleast one element in array matches condition | |
let hasUserLessThan18 = users.some(function(user) { | |
return user.age < 18; | |
}); | |
console.log(hasUserLessThan18); // false | |
// every - to check that ALL elements in array matches condition | |
let allUsersMoreThan18 = users.every(function(user) { | |
return user.age > 18; | |
}); | |
console.log(allUsersMoreThan18); // true | |
// *************************************************************************** | |
// filter | |
let users = [ | |
{ id: 1, name: "John", age: 43 }, // same age | |
{ id: 2, name: "Pete", age: 43 }, | |
{ id: 3, name: "Mary", age: 37 } | |
]; | |
// returns new array that will contain only items for which function returned 'true' | |
let usersWithAge43 = users.filter(function(item) { | |
return item.age == 43 | |
}); | |
console.log(usersWithAge43.length); // 2 | |
console.log(usersWithAge43) | |
/* | |
[ { id: 1, name: 'John', age: 43 }, | |
{ id: 2, name: 'Pete', age: 43 } ] | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment