Last active
May 23, 2022 00:04
-
-
Save anibal21/ea78cf16546ac9ec51d073a994732b3d to your computer and use it in GitHub Desktop.
Javascript - The definitive guide - David Flanagan - Arrays Chapter
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
/** | |
* Theory: | |
-Arrays in JavaScript are an ordered collection of values | |
-Its elements are untyped, an array can contain different types of data | |
-Arrays are 32-bit indexed so its index can be from 0-4294967294 (2^32 - 2) | |
and its elements can go from 0 to 4.294.967.295 | |
*/ | |
/** | |
* Relevant Examples | |
*/ | |
// Dynamic Sizing | |
let names = ["Anibal", "Peter"] | |
console.log(names.length) // 2 | |
names[10] = "Lucas" | |
console.log(names.length) // 11 | |
//Creating arrays | |
let literalArray = [1,2,3] | |
let spreadOperatorArrays = [...literalArray] // copy of [1,2,3] | |
let arrayConstructor = new Array() | |
let arrayOf = Array.of(1,2,3) // [1,2,3] | |
let arrayFrom = Array.from(literalArray) // copy of [1,2,3] | |
// Creating and Updating at the same time | |
let rankings = [0, 100, 40, 20, 40, 343, 32, 22] | |
let copy = Array.from(rankings, (r) => r % 10 === 0 ? r : 0) | |
console.log(copy) // [0, 100, 40, 20, 40, 0, 0, 0] | |
// Some and Every | |
let yearTemperature = [40, 44, 40, 34, 30, 28, 26, 25, 28, 39, 35, 38] | |
console.log(yearTemperature.some((temp) => temp > 40)) // There is at least one element over 40 degrees | |
console.log(yearTemperature.every((temp) => temp % 2)) // false - not every number is mod 2 | |
// Find and FindIndex | |
let yearTemperature = [40, 44, 40, 34, 30, 28, 26, 25, 28, 39, 35, 38] | |
console.log(yearTemperature.find((temp) => temp === 40)) // 40 the first value | |
console.log(yearTemperature.findIndex((temp) => temp === 40)) // 0 the first encountered | |
/* End*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment