Methods for working with arrays

Array.from()

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

Array.prototype.every()

This method is used to test the array, it checks if all members of the array satisfy a condition (a condition within the callback function) and returns a boolean value. The method does not modify the source string. Three parameters are passed to the callback function of the method:

  • 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

Array.prototype.filter()

This method also returns a new array, but only with elements that passed the test implemented in the selected function. The method does not modify the source string. Three parameters are passed to the callback function of the method:

  • 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

Example

Example

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 instring.

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.forEach()

This method executes a specific function (callback) using each element of the passed array. This function is less flexible than the standard “for loop” because it has no conditional statements and there is no way to terminate a “loop” once started other than throwing an exception. 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. The function itself does not automatically return anything, so it is not “chainable” (the next method cannot be chained to it). Even after explicitly using the return statement, it will not return anything after the iteration.

Example

The same result can be obtained using other attributes

Example

Callback function cannot use “empty array members” (array member with an index that has no associated value) in the calculation. Therefore, in the following example, this method will not take into account an empty element, so the array member with index 2 will be skipped:

Array.prototype.from()

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

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.includes()

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

Array.prototype.map()

This method returns a new array, which is obtained by using a callback function on the elements of the original array paying attention to the order of the members of the original array. As with the forEach() function, the callback function does not use “empty string members” (a string member with an index that has no associated value) in the calculation. The map() function does not affect the source string. The method passes three parameters to the callback function:

  • string member value
  • 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

Example

NOTE:
Pay attention to the case when functions that require clearly defined parameters are used for callback functions, and not the mentioned three parameters passed by the map() function.
The best example is the parseInt() function, which requires a string for the first parameter and a numeric system for the second.

The parseInt() function returns NaN when it receives an array index from the map() method for the second parameter (which looks for a numeric system type, expected numbers are: 2, 8, 10, 16…).