Let's use reduce, map, filter, foreach and be more functional in javascript

lechatthecat

lechat

Posted on April 16, 2021

Let's use reduce, map, filter, foreach and be more functional in javascript
console.log([1,2,3,4].reduce((x,y) => { return x  + y })) // 10 
// Elements reduced to one value

console.log([1,2,3,4].map((x) => { return x  + 5 })) // [6,7,8,9] 
// Do something for each value and make a new array

console.log([1,2,3,4].filter((x) => { return x  % 2 === 0 })) // [2,4] 
// Filter the elements and make a new array

const array1 = [1, 2, 3, 4];

array1.forEach(x => { console.log(x)});
// For each loop.

Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
lechatthecat
lechat

Posted on April 16, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

How to Use Head and Tail in Elixir
elixir How to Use Head and Tail in Elixir

January 25, 2023

Your First Elixir Project (Part 2)
elixir Your First Elixir Project (Part 2)

January 5, 2022

Variable on Golang
go Variable on Golang

October 30, 2021