Asabeneh/Functional-Programming-in-JavaScript là dự án mã nguồn mở trên GitHub, viết chủ yếu bằng JavaScript, với 127 sao. A complete summary of functional programming in JavaScript
Tóm tắt dựng từ metadata GitHub của chính dự án — chưa có bài review TopGit. Trang sẽ tự động cập nhật khi bài review đầy đủ được xuất bản.
VÌ SAO CHƯA CÓ REVIEW
TopGit viết bài đầy đủ cho repo có nhiều sao nhất và được yêu cầu nhiều nhất. Trang này là snapshot trong thời gian chờ — xem README gốc ở tab READ ME.
Support Asabeneh to create more educational materials
1. forEach
2. map
3. filter
4. reduce
5. find
6. findIndex
7. some
8. every
Exercise
More Materials
In this article, I will try to help you to have a very good understanding of the most common feature of JavaScript, functional programming.
Functional programming allows you to write shorter code, clean code, and also to solve complicated problems which might be difficult to solve in a traditional way.
For more JavaScript and other programming lessons and tutorials, you may check Washera YouTube channel.
In this article we will cover all JS functional programming methods:
forEach
map
filter
reduce
find
findIndex
some
every
1. forEach
We use forEach when we like to iterate through an array of items. The forEach is a higher-order function and it takes call-back as a parameter. The forEach method is used only with array and we use forEach if you are interested in each item or index or both.
// syntax in a normal or a function declaration
function callback(item, index, arr) {}
array.forEach(callback)
// or syntax in an arrow function
const callback = (item, i, arr) => {}
array.forEach(callback)
The call back function could be a function declaration or an arrow function.
If there is no much code inside the code block we can use an arrow function and we can write it without a curly bracket. The index and the array parameters are optional, we can omit them.
Let us see more examples. For instance if we want to sum an array of numbers we can use forEach or reduce. Let us see how we use forEach to sum all numbers in an array.
const numbers = [1, 2, 3, 4, 5]
let sum = 0
numbers.forEach((n) => sum += n)
console.log(sum) // 15
Let us move to the next functional programming method which is going to be a map.
2. map
We use the map method whenever we like to modify an array. We use the map method only with arrays and it always returns an array.
// syntax in a normal or a function declaration
function callback(item, i) {
return // code goes here
}
const modifiedArray = array.map(callback)
// or syntax in an arrow function
const callback = (item, i) => {
return // code goes here
}
const modifiedArray = array.map(callback)
Now, let us modify the countries array using the map method. The index is an optional parameter
// Using function declaration
const countries = ['Finland', 'Estonia', 'Sweden', 'Norway']
const newCountries = countries.map(function(country) {
return country.toUpperCase()
})
console.log(newCountries)
// map using an arrow function call back
const countries = ['Finland', 'Estonia', 'Sweden', 'Norway']
const newCountries = countries.map(country => country.toUpperCase())
console.log(newCountries) // ["FINLAND", "ESTONIA", "SWEDEN", "NORWAY"]
As you can see that map is very handy to modify an array and to get an array back. Now, let us create an array of the length of the countries from the countries array.
As you may understand from the literal meaning of filter, it filters out items based on some criteria. The filter method like forEach and map is used with an array and it returns an array of the filtered items.
For instance if we want to filter out countries containing a substring land from an array of countries. See the example below:
// syntax in a normal or a function declaration
function callback(item) {
return // boolean
}
const filteredArray = array.filter(callback)
// or syntax in an arrow function
const callback = (item) => {
return // boolean
}
const filteredArray = array.filter(callback)
Now, you know how to filter let us move on to the next functional programming, reduce.
4. reduce
Like forEach, map, and filter, reduce is also used with an array and it returns a single value. You can associate reduce with a blender. You put different fruits to a blend and you get a mix of fruit juice. The juice is the output from the reduction process.
We use the reduce method to sum all numbers in an array together, or to multiply items in an array or to concatenate items in an array. Let us see the following different example to make this explanation more clear.
// syntax in a normal or a function declaration
function callback(acc, cur) {
return // code goes here
}
const reduced = array.reduce(callback, optionalInitialValue)
// or syntax in an arrow function
const reduced = callback(acc, cur) => {
return // code goes here
}
const reduced = array.reduce(callback)
The default initial value is 0. We can change the initial value if we want to change it.
For instance if we want to add all items in an array and if all the items are numbers we can use reduce.
If we are interested in the first occurrence of a certain item or element in an array we can use find method. Unlike map and filter, find just return the first occurrence of an item instead of an array.
// syntax in a normal or a function declaration
function callback(item) {
return // code goes here
}
const item = array.find(callback)
// or syntax in an arrow function
const reduced = callback(item) => {
return // code goes here
}
const item = array.find(callback)
Let find the first even number and the first odd number in the numbers array.
Let us find the first country which has only six characters
const countries = ['Finland', 'Estonia', 'Sweden', 'Norway', 'Iceland']
const sixCharsCountry = countries.find((country) => country.length === 6)
console.log(sixCharsCountry) // Sweden
Let us find the first country in the array which has the letter 'o'
const countries = ['Finland', 'Estonia', 'Sweden', 'Norway', 'Iceland']
const index = countries.find((country) => country.includes('o'))
console.log(index // Estonia
6. findIndex
The findIndex method works like find but findIndex returns the index of the item. If we are interested in the index of a certain item or element in an array we can use findIndex. The findIndex return the index of the first occurrence of an item.
// syntax in a normal or a function declaration
function callback(item) {
return // code goes here
}
const index = array.findIndex(callback)
// or syntax in an arrow function
const reduced = callback(item) => {
return // code goes here
}
const index = array.findIndex(callback)
Let us find the index of the first even number and the index of the first odd number in the numbers array.
Let us find the index of the first country in the array which has exactly six characters
const countries = ['Finland', 'Estonia', 'Sweden', 'Norway', 'Iceland']
const index = countries.findIndex((country) => country.length === 6)
console.log(index //2
Let us find the index of the first country in the array which has the letter 'o'.
const countries = ['Finland', 'Estonia', 'Sweden', 'Norway', 'Iceland']
const index = countries.findIndex((country) => country.includes('o'))
console.log(index // 1
Let us move on to the next functional programming, some.
7. some
The some method is used with array and return a boolean. If one or some of the items satisfy the criteria the result will be true else it will be false. Let us see it with an example.
In the following array some numbers are even and some are odd, so if I ask you a question, are there even numbers in the array then your answer will be yes. If I ask you also another question, are there odd numbers in the array then your answer will be yes. On the contrary, if I ask you, are all the numbers even or odd then your answer will be no because all the numbers are not even or odd.
The price of banana is 3 euros.
The price of mango is 6 euros.
The price of potato is unknown.
The price of avocado is 8 euros.
The price of coffee is 10 euros.
The price of tea is unknown.
Calculate the sum of all the prices using forEach
Create an array of prices using map and store it in a variable prices
Filter products with prices
Use method chaining to get the sum of the prices(map, filter, reduce)
Calculate the sum of all the prices using reduce only
Find the first product which doesn't have a price value
Find the index of the first product which does not have price value
Check if some products do not have a price value
Check if all the products have price value
Explain the difference between forEach, map, filter and reduce
Explain the difference between filter, find and findIndex
Explain the difference between some and every
More Materials
If you want to dive deep into JavaScript, you can give it a try to the 30DaysOfJavaScript challenge. This challenge will take quite long time to finish but you can get all you need about JavaScript
JavaScript
30DaysJavaScript challenge
JavaScript for Everyone
JavaScript Loops
Destructuring in JavaScript
React
React for Everyone
Python
30DaysOfPython
🎉 CONGRATULATIONS 🎉
Now, you know everything you need to know about JavaScript functional programming methods.
Asabeneh/Functional-Programming-in-JavaScript có bao nhiêu sao?
Asabeneh/Functional-Programming-in-JavaScript có 127 sao GitHub — tải lại trang để xem số mới nhất, hoặc xem trực tiếp github.com/Asabeneh/Functional-Programming-in-JavaScript. TopGit phản chiếu số sao của GitHub nhưng không cam kết đến từng phút.
Asabeneh/Functional-Programming-in-JavaScript có phải mã nguồn mở không?
TopGit chưa ghi nhận license cho Asabeneh/Functional-Programming-in-JavaScript. Phần lớn repo public trên GitHub là mã nguồn mở, nhưng điều khoản khác nhau từng repo — mở file LICENSE để xác nhận.
Asabeneh/Functional-Programming-in-JavaScript có tag gì không?
Bản đồng bộ chưa ghi nhận topic GitHub nào cho Asabeneh/Functional-Programming-in-JavaScript. GitHub topics hiển thị ở thanh bên phải trang repo — đó là nơi đáng kiểm tra nhất.
Asabeneh/Functional-Programming-in-JavaScript có website riêng không?
TopGit chưa ghi nhận URL trang chủ cho Asabeneh/Functional-Programming-in-JavaScript. Phần README ở tab phía trên thường có link demo, hoặc xem mô tả GitHub của repo.
Asabeneh/Functional-Programming-in-JavaScript còn đang phát triển không?
Commit gần nhất trên Asabeneh/Functional-Programming-in-JavaScript là 3.1 năm trước (theo timestamp GitHub). Repo có 27 fork — một chỉ báo về mức độ quan tâm của cộng đồng.
Đọc thêm về Asabeneh/Functional-Programming-in-JavaScript ở đâu?
Trang TopGit này là một snapshot — tab "Readme" hiển thị nguyên văn README của repo (đã bỏ link, giữ ảnh). Repo GitHub ở github.com/Asabeneh/Functional-Programming-in-JavaScript là nguồn chính thức.
Đọc đầy đủ README ở tab phía trên.
Chưa chắc Functional-Programming-in-JavaScript có hợp với bạn?
Để ChatGPT, Claude hoặc Perplexity tìm hiểu giúp — bấm bên dưới và xem AI nói gì về Functional-Programming-in-JavaScript.