Array Creation
Array.of()
Uses all values in the arguments as elements to form an array.
Array.from()
Converts an array-like object or iterable object into an array.
Parameters
Array.from(arrayLike[, mapFn[, thisArg]])
The return value is the converted array.
arrayLike
The array-like object or iterable object to be converted.
mapFn
Optional. The map function, used to process each element; the processed elements are placed into the array.
console.log(Array.from([1, 2, 3], (n) => n * 2)); // [2, 4, 6]
thisArg
Optional. Used to specify the `this` object when executing the map function.
Array-like Objects
An array-like object must have a `length` property, and the element property names must be numeric or strings convertible to numbers.
Converting Iterable Objects
Converting Map
Converting Set
Converting Strings
Extended Methods
Find
find()
Finds an element in the array that meets the condition; if there are multiple matching elements, returns the first one.
findIndex()
Finds the index of an element in the array that meets the condition; if there are multiple matching elements, returns the index of the first one.
Fill
fill()
Fills array elements within a specified index range with a single specified value.
copyWithin()
Modifies array elements within a specified index range to the elements from another specified index range of the same array.
Iteration
entries()
Iterates over key-value pairs.
keys()
Iterates over keys.
values()
Iterates over values.
Includes
includes()
Whether the array contains the specified value.
Note: distinguish from the `has` methods of Set and Map; Set's `has` method is used to find values; Map's `has` method is used to find keys.
Flattening Nested Arrays into One-dimensional Arrays
flat()
First processes each element in the array, then executes the flat() method on the array.
ArrayBuffer
An ArrayBuffer is a segment of addresses in memory.
The foundation of typed arrays.
The actual number of bytes is determined at creation time. Afterwards, only the data within it can be modified; its size cannot be changed.
Creating an ArrayBuffer
Created via constructor:
View
A view is an interface used to operate on memory.
A view can operate on an ArrayBuffer or a subset of the buffer's bytes, and reads and writes data according to one of the numeric data types.
The DataView type is a generic ArrayBuffer view that supports all 8 numeric data types.
Creation:
Typed Arrays
Views of a specific type for the ArrayBuffer.
You can force the use of a specific data type instead of using a generic DataView object to operate on the ArrayBuffer.
Creation
Generated from an ArrayBuffer.
Via constructor.
Points to Note
The `length` property is not writable. If you try to modify this value, the operation is silently ignored in non-strict mode, and an error is thrown in strict mode.
Typed arrays can be iterated using entries(), keys(), and values().
Methods such as find() can also be used on typed arrays, but the methods of typed arrays additionally check whether the numeric type is safe, and also use Symbol.species to confirm that the return value of the method is a typed array rather than a normal array. The concat() method cannot be used on typed arrays because the result of merging two typed arrays is uncertain; in addition, since the size of a typed array cannot be changed, methods that can change the size of an array, such as splice(), are not applicable to typed arrays.
All typed arrays contain static of() and from() methods, whose behavior is similar to Array.of() and Array.from() respectively. The difference is that the typed array methods return a typed array, while the normal array methods return a normal array.
Typed arrays are not normal arrays and do not inherit from Array.
Typed arrays add the set() and subarray() methods. The set() method is used to copy other arrays into an existing typed array, and subarray() is used to extract a part of an existing typed array to form a new typed array.
Spread Operator
Copying Arrays
Merging Arrays