arrays
let listNum = [2, 3, 5, 7, 11];
console.log(listNum[2]);
console.log(listNum[0]);
console.log(listNum[2 - 1]);
length
Arrays contain a .length property which is a number (not a method) containing
the length of the array. To loop over an array you can use length - 1.
looping
In modern javascript there's some syntax sugar for looping over arrays.
// traditional
for (let i = 0; i < JOURNAL.length; i++) {
let entry = JOURNAL[i]
console.log(entry);
}
// shorthand
for (let entry of JOURNAL) {
console.log(entry);
}
random selection
function randomPick(array) {
let choice = Math.floor(Math.random() * array.length)
return array[choice]
}
push, pop, shift, unshift
Push adds a value to the end of an array. Pop removes the last value in an array (and returns it). Shift and unshift do the same things, but at the start of an array.
let sequence = [1, 2, 3];
sequence.push(4);
sequence.push(5);
console.log(sequence);
// [1, 2, 3, 4, 5]
console.log(sequence.pop());
// 5
console.log(sequence);
// [1, 2, 3, 4]
includes, indexOf, lastIndexOf, findIndex
Arrays have an includes() method that checks wether a given value exists in the
array. If you need an actual location rather than a true/false you can use
either indexOf() or lastIndexOf(). Both methods search for a value in an
array and return the index if it exists or -1 if it does not. lastIndexOf()
searches the array in reverse order. Both take an optional second argument which
allows specifying a different index to start searching from. Finally, there's
findIndex which takes a function and finds the first value in the array for
which the function returns true. It returns -1 if it doesn't find a match.
slice
Takes start and end indices and returns an new array containing the elements between them. If an end is not given it will take all elements after the start.
console.log([0, 1, 2, 3, 4.slice(2, 4));
// [2, 3]
console.log([0, 1, 2, 3, 4.slice(2));
// [2, 3, 4]
concat
This method will concatenate arrays together to create a new array. Similar to the + operator for strings. You can use this to create an array remove function for example.
function remove(array, index) {
return array.slice(0, index)
.concat(array.slice(index + 1));
}
console.log(remove(["a", "b", "c", "d", "e"], 2));
// ["a", "b", "d", "e"]
forEach
["a", "b"].forEach(l => console.log(l));
// a
// b
some
Takes a test function and tells you wether that function returns true for any of the elements in the array.
filter, map, reduce
filter an array with a functional test. A new array is created with only
elements that passed the filter.
map applies a function call to each element in an array. The returned values
are used to create a new array.
reduce combines an array down to a single element. A typical example is adding
up numbers, but any function may be used instead of addition. For each element
the combine action is applied to the current running sum and the current
element.
var SCRIPTS = [
{
name: "Adlam",
direction: "rtl",
year: 1987,
living: true,
link: "https://en.wikipedia.org/wiki/Fula_alphabets#Adlam_alphabet",
},
{
name: "Caucasian Albanian",
direction: "ltr",
year: 420,
living: false,
link: "https://en.wikipedia.org/wiki/Caucasian_Albanian_alphabet",
},
{
name: "Ahom",
direction: "ltr",
year: 1250,
living: false,
link: "https://en.wikipedia.org/wiki/Ahom_alphabet",
},
{
name: "Arabic",
direction: "rtl",
year: 400,
living: true,
link: "https://en.wikipedia.org/wiki/Arabic_script",
},
{
name: "Imperial Aramaic",
direction: "rtl",
year: 800,
living: false,
link: "https://en.wikipedia.org/wiki/Aramaic_alphabet",
},
{
name: "Armenian",
direction: "ltr",
year: 405,
living: true,
link: "https://en.wikipedia.org/wiki/Armenian_alphabet",
}
]
let rtl = SCRIPTS.filter((s) => s.direction == "rtl")
rtl.forEach((s) => console.log(s.name))
// Adlam
// Arabic
// Imperial Aramaic
// Use map to create an array with the rtl script's names.
console.log(rtl.map((s) => s.name))
// [ "Adlam", "Arabic", "Imperial Aramaic" ]
function characterCount(script) {
return script.ranges.reduce((count, [from, to]) => {
return count + (to - from)
}, 0)
}
function biggly(a, b) {
if (characterCount(a) < characterCount(b)) {
return b
} else {
return a
}
}
// Use reduce (twice) to find the script with the most characters. This will
// fail on the above sample because I removed the character ranges to save
// space.
console.log(SCRIPTS.reduce(biggly).name)
// Han
destructuring
// Clunky way to write this function.
function phi(table) {
return (table[3] * table[0] - table[2] * table[1]) /
Math.sqrt((table[2] + table[3]) *
(table[0] + table[1]) *
(table[1] + table[3]) *
(table[0] + table[2]));
}
// Using destructuring to create variables for elements in the array we accept.
function phi([n00, n01, n10, n11) {
return (n11 * n00 - n10 * n01) /
Math.sqrt((n10 + n11) * (n00 + n01) *
(n01 + n11) * (n00 + n10));
}
You can also use destructuring for variables created with let, var, const
if you know the array index.
let {name} = {name: "Kota", age: 23};
console.log(name);