|
for (key in obj)
|
Iterates over keys
|
|
for (value of obj)
|
Iterates over values
|
|
arr.forEach(function(val, I, arr))
|
Executes a function for each value.
|
|
arr.every(function(val, i, arr))
|
Returns true if every value passes the function test
|
|
arr.reverse()
|
Reverses the original array
|
|
Searching
|
|
|
arr.includes(val)
|
Returns true if array contains a value
|
|
arr.indexOf(val)
|
Returns the index of a value in an array or -1 if not found
|
|
arr.find(function)
|
Returns the value of the first index to satisfy the function
|
|
arr.findIndex(function)
|
Returns the index of the first value to satisfy the function
|
|
Add Values
|
|
|
arr.splice(start, 0, items...)
|
Adds items to array beginning at starting index
|
|
arr.unshift(val)
|
Adds val to front of array
|
|
arr.push(val)
|
Adds val to back of array
|
|
Delete Values
|
|
|
arr.splice(start, n)
|
Delete n items from array beginning at starting index
|
|
arr.shift()
|
Removes arr[0] and returns the value
|
|
arr.pop()
|
Removes last value in a array and returns it
|
|
Copy Values
|
|
|
arr.slice(beg, end)
|
Returns a shallow copy of arr from beg to end (non-inclusive)
|
|
To String
|
|
|
arr.join(separator)
|
Returns a string from the array values, with an optional separator string
|
|
JSON.stringify(arr)
|
Returns a JSON string representation of array. Deep string.
|
|
arr.toString()
|
Returns a comma-delimited string of scalar array values. Shallow string.
|
|
Sorting
|
|
|
arr.sort()
|
Sorts lexicographically
|
|
arr.sort((a, b) => a - b)
|
Sorts numbers
|
|
num.toFixed(n)
|
Returns a number rounded to n significant figures after the decimal
|
|
num.toPrecision(n)
|
Returns a string rounded to n significant figures
|
|
num.toString()
|
Returns a string representation of a number
|
|
Object to Array
|
|
|
Object.keys(obj)
|
Array of obj keys
|
|
Object.values(obj)
|
Array of obj values
|
|
Object.entries(obj)
|
2D array of key:value pairs
|
|
Object to String
|
|
|
JSON.stringify(obj)
|
obj to string
|
|
JSON.parse(obj)
|
string to obj
|
|
Add/Remove Properties
|
|
|
delete obj[key]
|
Delete property
|
|
obj[key] = value
|
Add property
|
|
str.slice(beg, end)
|
returns a substring from beginning to end index
|
|
str.toLowerCase()
|
Returns a lowercase version of str
|
|
String to Array
|
|
|
str.split(separator)
|
Returns an array of the string, split by the separator
|
|
Searching Strings
|
|
|
str.includes(str)
|
Returns true if str is in string
|
|
str.indexOf(str)
|
Returns index of str if within string, or -1 otherwise
|
|
str.startsWith(str, beg)
|
Returns true if string starts with str, optionally from beg index.
|
|
str.endsWith(str, len)
|
Returns true if string starts with str, optionally limited to the first len characters
|
|
Combine Strings
|
|
|
str.concat(strings...)
|
Returns new string concatenation
|
|
Math.abs(num)
|
Returns the absolute value of num
|
|
Random Numbers
|
|
|
Math.round(Math.random() * (max - min)) + min)
|
Returns a random number between two ints
|
|
Math.random()
|
Returns a random number between 0 and 1.
|
|
Rounding
|
|
|
Math.floor(num)
|
Goes down to the nearest int <= num
|
|
Math.ceil(num)
|
Goes up to the nearest int >= num
|
|
Math.round(num)
|
Rounds to the nearest int
|
No comments yet.
Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.