Kristoffer Brabrand
Senior developer @ Behalf
creates a new array with the result of calling the provided function for each value in the array
const list = [1, 2, 3, 4]
const squares = list.map(a => a * a)
console.log(squares) // [1, 4, 9, 16]creates a new array including only the values where the function returns a truthy value when called with the value
const list = [1, 2, 3, 4, 5]
const evenNumbers = list.filter(
number => number % 2 === 0
)
console.log(evenNumbers) // [2, 4]incrementally build a value while iterating over the values in the array, passing the result of the last step into the next
const list = [1, 2, 3, 4]
const sum = list.reduce(
(res, value) => res + value,
0
)
console.log(sum) // 10find the first element in an array that the function returns a truthy value for
const animals = ['š¶', 'š', 'š±']
const cat = animals.find(
animal => animal === 'š±'
)
console.log(cat) // 'š±'find the index of the first element in an array that the function returns a truthy value for
const animals = ['š¶', 'š', 'š±']
const catIndex = animals.findIndex(
animal => animal === 'š±'
)
console.log(catIndex) // 2By Kristoffer Brabrand