New methods for working with arrays

New methods for working with arrays

Array.from()

Using the method “from()” we can make an array from “array-like” or “iterable”:

ES2015

Array.prototype.find()

This method returns the first member of the array that satisfies the condition (the condition within the callback function). The method passes three parameters to the callback function:

  • string member value
  • index of array member
  • an array (on which the method is executed)

In addition to the callback function, the argument thisArg can (optionally) be passed, which defines what the reserved word this will point to within the callback function.

Example

The findIndex() method does a similar thing, except that instead of the first element that met the condition, it returns its index in the array.

Array.prototype.findIndex()

This method returns the index of the first element in the array that satisfies the test function, if there is no such element then it returns “-1”.

Example

Array.prototype.includes()

The method that arrived with the ES2016 standard a checks if the requested element exists in the array.