Skip to main content

Array iteration methods(map, filter, reduce)

@serverSerrverlesskiy

The JavaScript language has a clear preference for arrays over other data structures. They have a lot of convenient specific tricks, for example, a whole set of iterating methods: map, filter, reduce.

map#

Create

The map () method creates🏗️ a new с array with the result of calling the specified function⚙️ for each element of the array.

Syntax#

Book

let new_array = arr.map(function callback( currentValue[, index[, array]]) {
// Returns the item for new_array
}[, thisArg])

The map method calls the passed callback function once for each element, in the order of their occurrence, and constructs a new array from the results of its call. The callback function is called only for array indices that have assigned values, including undefined. It is not called for missing array elements (that is, for indices that were never specified, deleted, or never assigned a value).

The function⚙️ callback is called with three arguments:

  • the value of the element,
  • element index
  • and the array through which the passage is carried out.

If the thisArg parameter was passed to the map method, it will be used as the this value when callback is called. Otherwise, the this value will be undefined. Ultimately, the value of this as seen from the callback function is determined according to the usual rules for defining this as seen from a function.

The map method does not modify the array for which it was called (although the function⚙️ can do that!).

The range of elements processed by the map method is set before the first call to the callback function. Items added to the array after the start of the map method will not be visited by the callback function. If the existing elements of the array are modified by the callback function, their values ​​passed to the function will be the values ​​at the time when the map method visits them. Deleted items will not be visited.

Payment#

Now you are on a stripped-down version of the site, after subscribing to Patreon, you will get full access to the training course, as well as access to our server's private channels in Discord.

Download our mobile application or get tested in our JavaScript telegram bot, and also subscribe to our news.

Become a Patron!

Sumerian school

Links#

  1. Simplify your JavaScript - use map, reduce and filter
  2. 15 useful javascript examples of map, reduce and filter
  3. Array.prototype.map()
  4. Array.prototype.filter()
  5. Array.prototype.reduce()

Contributors ✨#

Thanks goes to these wonderful people (emoji key):


AlisaNasibullina

📖

Dmitriy Vasilev

💵

Resoner2005

🐛 🎨 🖋

Navernoss

🖋 🐛 🎨

Become a Patron!