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 as this when executing mapFn

Return

A new array instance

Notes

  • Lets you create Arrays from:
    • array-like objects
      • those with a length property and indexed elements like arguments or NodeList
    • iterable objects
      • objects that have implemented the @@iterator method, like Map, Set, String, Array
  • This is the official replacement for [].slice.call(arrLike)
  • Array.from(obj, mapFn, thisArg) has same result as Array.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 new Uint8Array
  • Converts holes to value undefinedU
  • 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

<small>Research HTMLCollection, FileList, NodeList - all these elements that are array-like</small>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Array.isArray()

A

Array Static Method

Array.isArray(obj)

Parameters

  • obj - the object to be checked

Return

  • true if array; otherwise false

Notes

  • Always use Array.isArray() rather than instanceof because instanceof does not work through iframes
  • Always returns false for instances of TypedArray
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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 new Array with one element of which it’s value is 3. [3]
  • Array(3) will create an empty Array of length 3.
  • Polyfill is simply: Array.prototype.slice.call(arguments)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Array Static Methods

A

Static methods on the Array class:

from()

isArray()

of()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly