Skip to content

Latest commit

 

History

History
33 lines (20 loc) · 1.29 KB

12-eng.md

File metadata and controls

33 lines (20 loc) · 1.29 KB

Array Methods

So far we have gone over the following ways to iterate through an array: while and for loops, and the forEach, find and some methods.

Map

The map method is used to generate a new array with the same number of items as the original array, but with each item undergoing some transformation. Map goes through each item in the array, performs some transformation using the function passed as an argument, and returns a new array. It does not modify the original array. It must always return a value.

const numbers = [1, 2, 3]

//it can take three parameters: the item, the index, and the array itself. Item is the only required argument.

const doublenumbers = numbers.map(item => item * 2) 

console.log(doubleNumbers)
//[2, 4, 6]

Filter

Filter also iterates through an array, takes a function as an argument and executes for each item. When to use it? When based on a condition, you need to obtain an array with only a few items from the original array. The condition must always return a boolean.

const randomNumbers = [36, 99, 37, 63]

//it can take three parameters: the item, the index, and the array itself. Item is the only required argument.

const numbersGreaterThan37 = randomNumbers.map(item => item > 37) 

console.log(doublerandomNumbersNumbers)
//[99, 63]