Arrays::Static Methods Flashcards
1
Q
Array.from()
A
Array Static Method
Array.from(arrayLike[, mapFn[, thisArg]])
Parameters
-
arrayLike
- an array-like or iterable object to convert to an arrary. ex.arguments
,document.getQuerySelectorAll()
-
mapFn
- map function to call on every element - results will be applied to the new array. -
thisArg
- value to use asthis
when executingmapFn
Return
A new array instance
Notes
- Lets you create
Array
s from:- array-like objects
- those with a
length
property and indexed elements likearguments
orNodeList
- those with a
- iterable objects
- objects that have implemented the
@@iterator
method, likeMap
,Set
,String
,Array
- objects that have implemented the
- array-like objects
- This is the official replacement for
[].slice.call(arrLike)
-
Array.from(obj, mapFn, thisArg)
has same result asArray.from(obj).map(mapFn, thisArg)
except that it doesn’t create an intermediate array.- Not creating an intermediate array is useful if you were doing
Uint8Array.from(
on values that would be truncated, unless you used the former version which maps the values before creating the newUint8Array
- Not creating an intermediate array is useful if you were doing
- Converts holes to value
undefined
U - Should only use on array-like objects to create new array from it (with possible
mapFn
).- For creating new array from iterable objects, use spread syntax instead
...arr
- For creating new array from iterable objects, use spread syntax instead
<small>Research HTMLCollection, FileList, NodeList - all these elements that are array-like</small>
2
Q
Array.isArray()
A
Array Static Method
Array.isArray(obj)
Parameters
-
obj
- the object to be checked
Return
-
true
if array; otherwisefalse
Notes
- Always use
Array.isArray()
rather thaninstanceof
becauseinstanceof
does not work through iframes - Always returns
false
for instances ofTypedArray
3
Q
Array.of()
A
Array Static Method
Array.of(element0[, element1[, ...[, elementN]]])
Parameters
-
elementN
- elements with which to create the new array
Return
- A new array instance
Notes
-
Array.of(3)
will create a newArray
with one element of which it’s value is 3.[3]
-
Array(3)
will create an emptyArray
oflength
3. - Polyfill is simply:
Array.prototype.slice.call(arguments)
4
Q
Array Static Methods
A
Static methods on the Array class:
from()
isArray()
of()