Legendary methods of Array in JavaScript | Part 2 : reduce, some and every
ForEach, Filter, Find, Map, Reduce, Some and Every are all JavaScript array methods that are used to manipulate arrays and perform various operations on them. In this blog, we will take a look at what each of these methods does, how they can be used and why they are useful for web developers.
In this blog I will discuss reduce, some, and every.
.reduce 🔼
The reduce
method is used to reduce an array to a single value. This single value can be of any data type, such as a number, string, object, or even another array. The reduce
method works by iterating over an array and updating a "running total" with each iteration.
For example, let’s say you have a list of 💰 expenses and you want to find the total amount spent. You can use the reduce
method to achieve this:
let expenses = [
{ category: 'groceries', amount: 50 },
{ category: 'entertainment', amount: 30 },
{ category: 'transportation', amount: 20 },
{ category: 'utilities', amount: 40 },
{ category: 'rent', amount: 1000 }
];
let totalExpense = expenses.reduce((sum, expense) => sum + expense.amount, 0);
console.log(totalExpense);
/*
1140
*/
In this example, the reduce
method iterates over the expenses
array and updates the sum
variable with each iteration. The sum
variable starts at 0, and with each iteration, the amount of the current expense is added to the sum
. The final result is the total amount spent, which is 1140.
.some🤔
The some
method is used to check if at least one element in an array satisfies a certain condition. The condition is defined in a callback function that you provide as an argument. The some
method returns true
if at least one element in the array satisfies the condition, and false
otherwise.
For example, let’s say you have a list of 📚 books and you want to check if at least one book is rated 5 stars. You can use the some
method to achieve this:
let books = [
{ title: 'The Great Gatsby', stars: 4 },
{ title: 'To Kill a Mockingbird', stars: 5 },
{ title: 'Pride and Prejudice', stars: 4 },
{ title: 'The Lord of the Rings', stars: 5 },
{ title: 'The Hobbit', stars: 4 }
];
let hasFiveStarBook = books.some(book => book.stars === 5);
console.log(hasFiveStarBook);
/*
true
*/
In this example, the some
method iterates over the books
array and checks if at least one book has 5 stars. If at least one book has 5 stars, the some
method returns true
, and if not, it returns false
. In this case, the result is true
because there is at least one book rated 5 stars.
.every 💯
The every
method is used to check if all elements in an array satisfy a certain condition. The condition is defined in a callback function that you provide as an argument. The every
method returns true
if all elements in the array satisfy the condition, and false
otherwise.
For example, if you have a list of 🛍️ products and you want to check if all of them are in stock, you can use the every
method:
let products = [
{ name: '🍎', inStock: true },
{ name: '🍌', inStock: false },
{ name: '🥕', inStock: true },
{ name: '🍩', inStock: true },
{ name: '🍆', inStock: false }
];
let areAllProductsInStock = products.every(product => product.inStock === true);
console.log(areAllProductsInStock);
/*
false
*/
The every
method iterates over the products
array and checks if all products have an inStock
value of true
. If all products have an inStock
value of true
, the every
method returns true
. If at least one product has an inStock
value of false
, the every
method returns false
. In this case, the result is false
because not all products are in stock.
Some Quick Links
JsFramework | CleanCode | LocalStorage | Redis Caching | Node.js Cluster | Array Methods | Callback Function | Promise | Async/Await