JavaScript CheatSheet Flashcards
cheat sheet
What are the (7) Array finding functions?
.indexOf()
.lastIndexof()
.length()
.at()
.find()
.findIndex()
.includes()
What are the (4) Creating array functions
.slice()
.of()
.flat()
.flatMap()
What are the (6) Adding array functions?
.splice()
.push()
.copyWithin()
.fill()
.concat()
.unshift()
Name (3) Removing array functions
.shift()
.pop()
.splice()
Name (2) Re-arranging array functions
.reverse()
.sort()
What are (4) functions that can be used for Misc. tasks?
.join()
.toString()
.isArray()
.indexOf()
What are (8) looping array functions
.filter()
.map()
.reduce()
.forEach()
.reduceRight()
.some()
.every()
.entries()
Adding Methods
.push()
.unshift()
.splice()
Reorganizing methods
.copyWithin()
.slice()
.splice()
Combining methods
.concat()
…
Removing methods
.pop()
.shift()
.splice()
Filter/Searching methods
.filter()
.find()
.findIndex()
.indexOf()
.lastIndexOf()
Sort alphabetically.
let a = ['d', 'j', 'a', 'b', 'c', 'g'] a.sort() console.log(a) // ["a", "b", "c", "d", "g", "j"]
Sort in reversed alphabetical order.
let a = ['d', 'j', 'a', 'b', 'c', 'g'] a.sort().reverse() console.log(a) // [ "j", "g", "d", "c", "b", "a"]
Sort numerically.
let a = [5, 10, 7, 1, 3, 2] a.sort((a, b) => a - b) console.log(a) // [ 1, 2, 3, 5, 7, 10]
Descending numerical sort (flip the a
and b
around).
let a = [5, 10, 7, 1, 3, 2] a.sort((a, b) => b - a) console.log(a) // [10, 7, 5 ,3, 2, 1]
Add anything to the end of an array.
.push()
let a = [] let b = {} a.push(b) console.log(a[0] === b) // true
Add more than one item at a time to the end of an array
.push()
let x = ['a'] x.push('b', 'c') console.log(x) // ['a', 'b', 'c']
Add to the beginning of array and returns the new length of the array.
.unshift()
let x = ['c', 'd'] x.unshift('a', 'b') // 4 console.log(x) // ['a', 'b', 'c', 'd']
Add element to an arbitrary location (second param 0).
.splice()
let a = [1, 2, 3, 4] a.splice(2, 0, 'foo') console.log(a) // [1, 2, "foo", 3, 4]
Copy part of an array to another location within the same array.
.copyWithin()
let a = ['a', 'b', 'c', 'd', 'e'] // target, start, end array1.copyWithin(0, 3, 4) console.log(a) // ["d", "b", "c", "d", "e"]
Returns a portion of an array selected with the start
and end
parameters.
.slice()
let a = ['ant', 'bison', 'camel', 'duck', 'elephant'] console.log(animals.slice(2)) // ["camel", "duck", "elephant"] console.log(animals.slice(2, 4)) // ["camel", "duck"]
Replace an arbitrary element in array (second param 1).
.splice()
let a = [1, 2, 3, 4] a.splice(2, 1, 'foo') console.log(a) // [1, 2, "foo", 4]
Create a new array from two arrays.
.concat()
let x = ['a', 'b', 'c'] let y = ['d', 'e', 'f'] let z = x.concat(y) console.log(z) // ['a', 'b', 'c', 'd', 'e', 'f']
Not a method, but create a new array from two arrays by spreading them.
...
let x = ['a', 'b'] let y = ['c', 'd'] let z = [...x, ...y] console.log(z) // ['a', 'b', 'c', 'd']
Removes the last element from array and returns it. Changes the length of the array.
.pop()
let a = ['a', 'b', 'c'] console.log(a.pop()) // c console.log(a) // ["a", "b"]
Like pop
but removes the first element from array and returns it. Also** changes the length** of the array.
.shift()
let a = ['a', 'b', 'c'] console.log(a.shift()) // a console.log(a) // ["b", "c"]
Remove elements from the middle of an array. Param 1
: index to start removing. Param 2
: index to stop removing.
.splice()
let a = ['a', 'b', 'c', 'd', 'e'] a.splice(1, 3) console.log(a) // ["a", "e"]
Keeps the items in the array that pass the test provided by the callback function.
.filter()
let a = ['foo', 'bar', 'fooz'] let b = a.filter(v => v.startsWith('foo')) console.log(b) // ['foo', 'fooz']
Finds the first item in an array that matches.
.find()
let a = [5, 12, 8, 130, 44] let b = a.find(v => v > 10) console.log(b) // 12
Like find
but instead of returning the item, it returns the index.
.findIndex()
let a = [5, 12, 8, 130, 44] let b = a.findIndex(v => v > 13) console.log(b) // 3
Finds the first index of an element. Returns ` -1` if not found.
.indexOf()
let a = ['foo', 'bar', 'baz'] console.log(a.indexOf('baz')) // 2 console.log(a.indexOf('quix')) // -1
Like indexOf
, but finds the last index of an element. Returns -1
if not found.
.lastIndexOf()
let a = ['foo', 'bar', 'baz', 'foo'] console.log(a.lastIndexOf('foo')) // 3 console.log(a.lastIndexOf('quix')) // -1
Maps an array to another array by executing a callback function on each element.
.map()
let a = [1, 2, 3, 4] let b = a.map(v => v * 2) console.log(b) // 2, 4, 6, 8
Same as map
but it flattens the array, same as: [[]].map(v => v).flat()
.
.flatMap()
let a = [1, 2, 3, 4] let b = a.flatMap(v => [v * 2]) console.log() // 2, 4, 6, 8
Executes a function for every element in array, does not return anything.
.forEach()
let a = [1, 2] a.forEach(x => console.log(x)) // 1 // 2
Executes a user-supplied “reducer” callback function on each element of the array, in order, passing in the return value from the calculation on the preceding element.
.reduce()
let a = [1, 2, 3, 4] let b = a.reduce((acc, v) => acc + v) // Basically: 1 + 2 + 3 + 4 console.log(b) // 10
Like reduce
but reads the array from right to left.
.reduceRight()
let a = [1, 2, 3, 4] let b = a.reduce((acc, v) => acc + v) // Basically: 1 + 2 + 3 + 4 console.log(b) // 10
Tests if every item in the array passes the test.
.every()
let isBelow = v => v < 40 let a = [1, 30, 39, 29, 10, 13] console.log(a.every(isBelow)) // true
Tests if some items in the array passes the test.
.some()
~~~
let isOver = v => v > 40
let a = [1, 30, 41, 29, 10, 13]
console.log(a.some(isOver)) // true
~~~
Test if an array contains a value.
.includes()
let a = [1, 2, 3] let b = a.includes(2) console.log(b) // true
Creates a new array from the given arguments.
Array.of()
~~~
Array.of(‘foo’, ‘bar’)
// [‘foo’, ‘bar’]
~~~
Creates a new array from an array-like or iterable object.
Array.from()
console.log(Array.from('foo')) // ['f', 'o', 'o'] console.log(Array.from([1, 2, 3], x => x + x)) // [2, 4, 6]
Change all elements in an array to a static value.
.fill()
let a = [1, 2, 3, 4] let b = a.fill(0, 2, 4) console.log(b) // [1, 2, 0, 0]