Slide navigation: Forward with space bar, →, or PgDn. Backwards with ← or PgUp.
Array(length)or
new Array(length)[element1, element2, ...] Array(element1, element2, ...) new Array(element1, element2, ...)
Array.of(element1, ...) works with single integer elementArray.from(iterable) like [...iterable]Array.from(iterable, mapFun, thisArg) transforms the source elements:
Array.from('Hello', x => x.toUpperCase())
→ [ 'H', 'E', 'L', 'L', 'O' ]
x = arr.pop(), arr.push(x): Removes, adds last element.x = arr.shift(), arr.unshift(x): Removes, adds first element.deleted = arr.splice(start, deleteCount, x1, x2, ...)
deleteCount > 0, elements are deleted at startx1, x2, ... are present, they are inserted at startdeleteCount is absent, all element from start on are deleted.arr.copyWithin(target, start, end)
target, start, end < 0, they are counted from the end.start defaults to 0, end to arr.length.const arr = [0, 1, 4, 9, 16, 25] arr.copyWithin(0, 1) //arris now[1, 4, 9, 16, 25, 25]arr.copyWithin(1) //arris now[1, 1, 4, 9, 16, 25]
length property, you can resize the array.arr.fill(value, start, end)
start defaults to 0, end to arr.length.arr.reverse() reverses arr in place and returns arrarr.sort(compareFunction) sorts arr in place and returns arr
const arr = [0, 1, 16, 25, 4, 9] arr.sort((x, y) => x - y) //arris now[0, 1, 4, 9, 16, 25]
x, y and returns
x should come before yx should come after yconst arr = [0, 1, 4, 9, 16, 25] arr.sort() //arris now[0, 1, 16, 25, 4, 9]
arr.includes(target, fromIndex), arr.indexOf(target, fromIndex), arr.lastIndexOf(target, fromIndex) searches for target
fromIndex defaults to 0fromIndex < 0, counts from the end of the array.arr.slice(begin, end) yields a shallow copy of the given range.
begin defaults to 0, end to arr.lengtharr.slice() is the same as [...arr]arr.concat(value1, value2, ...) yields an array starting with arr, to which the values are concatenated.
[1, 2, 3].concat(4, {age: 5}, [6, 7]) → [ 1, 2, 3, 4, { age: 5 }, 6, 7 ]arr.join(separator) yields a string joining all elements, using the given separator
','[1,2,3,[4,5]].join(' and ') → '1 and 2 and 3 and 4,5'arr.flat(levels) flattens multidimensional arrays. The default is to flatten one level.
[[1, 2], [3, 4]].flat() → [1, 2, 3, 4]
arr.keys(), arr.values(), arr.entries() yield iterables to the index values, elements, and [index, element] pairs.
const arr = [0, 1, 4, 9] [...arr.entries()] → [[0, 0], [1, 1], [2, 4], [3, 9]]
arr.forEach(f) calls f(element, index, arr) for each element.
thisArg argument that sets this in farr.map(f) yields an array of all values returned from f(element, index, arr)arr.flatMap(f) flattens the values returned from f(element, index, arr) and yields their concatenation.arr.filter(f) yields an array of all values for which f(element, index, arr) is truearr.every(f), arr.some(f) yields true if f(element, index, arr) is true for every or at least one element.arr.find(f), arr.findIndex(f) finds the first element, or its index, for which f(element, index, arr) is truea.reduce(f) computes a value from the elements of a, using f to combine them as
f(...f(f(a[0], a[1]), a[2])...,a[n-1])
[1, 2, 3, 4].reduce((x, y) => x + y) → 10 // ((1 + 2) + 3) + 4 [1, 2, 3, 4].reduce((x, y) => 10 * x + y) → 1234 [1, 9, 7, 2].reduce((x, y) => Math.max(x, y)) → 9
a.reduce(f, init) computes f(...f(f(init, a[0]), a[1])...,a[n-1])
[1, 2, 3, 4].reduce((x, y) => x - y, 0) → -10 // (((0 - 1) - 2) - 3) - 4
reduceRight associates from the right in reverse order: f(f(...f(a[n - 1], a[n - 2])..., a[1]),a[0])
[1, 2, 3, 4].reduceRight((x, y) => [x, y]) → [[[4, 3], 2], 1]
Int8Array Uint8Array Uint8ClampedArray Int16Array Uint16Array Int32Array Uint32Array Float32Array Float64Array
Uint: unsigned integergetImageDataXMLHttpRequest, WebSockets, ...const buffer = ... const arr = new Uint16Array(buffer)
DataView instead:
const view = new DataView(buffer)
DataView methods getInt8, getInt16, getInt32, getUInt8, getUInt16, getUInt32, getFloat32, getFloat64:
let value = view.getUint32(offset, true)
true for little-endian byte order, false (the default) for big-endian.set method:
view.setUint32(offset, newValue, true)
for (const [key, value] of map)
=== except that all NaN are equal.new Map() makes empty map.new Map(iterable), where iterable produces an array of pairs [key, value]size property
map.set(key, value) adds the key/value pair, returns mapmap.delete(key) removes key and its associated value and returns true if the key was present, returns false otherwise.map.has(key), map.get(key) return true/the associated value if the key is present, false/undefined otherwise.map.clear() empties the map.map.forEach(f, thisArg) invokes f(key, value) on each element.keys, values, entries yield iterators over the keys, values, and [key, value] pairs.=== except that all NaN equal another.for (const element of set) ...
new Set() or new Set(iterable), where iterable produces elements.size property yields number of elements.set.add(x) adds x if not present and returns setset.delete(x) deletes x and returns true if x was present, returns false otherwise.set.has(x) tests whether x is present.set.clear() deletes all elements.set.forEach(f, thisArg) invokes f(element, element) on each element.keys, values, entries yield iterators over elements, elements, and pairs [element, element]WeakMap methods are set/delete/get/hasWeakSet methods are add/delete/has