Welcome back, developers! This is the second part of understanding how to use reduce method in JavaScript. Click here to check part 1 of the reduce method. Needless to say, this article is more of the same. We will go through various ways to see how JavaScript reduce method is implemented. So, let’s start.
When to Apply JavaScript Reduce Method?
JavaScript Reduce method is applicable in tons of places. Let’s take a look at a few of such examples
- Concatenating Nested List
- Removing Duplicate Using Reduce Method
- Reversing a String Using Reduce Method
Concatenating Nested List
let listOfLists = [
["My", "name", "is", "Daniyal"],
["Welcome", "to", "Programatically"],
["This", "is", "a", "developers", "community"],
["Here", "we", "help", "each", "other", "out"],
];
const singleList = listOfLists.map((item) =>
item.reduce((a, i) => `${a} ${i}`)
);
console.log(singleList);
// OUTPUT
[
'My name is Daniyal',
'Welcome to Programatically',
'This is a developers community',
'Here we help each other out'
]
In this one, you can see the initial nested list and the final single list. Using reduce method coupled with the map method in order to return the output in list format. Lambda function really takes makes the task even easier.
Removing Duplicate Using Reduce Method
listOfNumbers = [2, 4, 6, 8, 8, 10, 2, 4, 4, 6, 18, 20];
var newList = listOfNumbers.reduce((noDuplicates, item) => {
if (noDuplicates.indexOf(item) === -1) {
noDuplicates.push(item);
}
return noDuplicates;
}, []);
console.log(newList);
In here you can see we are initializing ‘noDuplicates’ variable as an empty list. Then checking individual elements from the ‘listOfNumbers‘ that whether we’ve already pushed it in ‘noDuplicates’ list. If not then we do push them, else we do nothing. In the end, ‘noDuplicates’ is returned and stored in ‘newList’ variable. This is how we remove duplicate values from a list using reduce method in JavaScript. Check the code in action shown below:
Reversing a String Using Reduce Method in JavaScript
const reverseIt = str=>[...str].reduce((a,b)=>b+a)
console.log(reverseIt("Programatically"));
//OUTPUT: yllacitamargorP
This is basically a function which you can call like so: reverseIt(“STRING”).
The string value that you will pass in the ‘reverseIt()’ method is being separated individually in a list using JavaScript Spread method. Then using the reduce method, each alphabet is being concatenated in reverse order. Its then printed on the console log.
That’s a wrap!
I hope you guys found this article helpful. Now you should have a better understanding of how to use reduce method in JavaScript. For more articles like this, leave your reviews in the comment section below.
Have a great one!
Recent Posts
- Haaienjacht op de rollen de spanning van de chicken road game casino
- Překvapivé příležitosti a herní svět goldzino – pro radost a napětí
- Fast Payout Casinos Ireland.2848
- Spinsy Casino France comment jouer dans un casino en ligne moderne.2354
- Casino Yep PL Kompletny przewodnik po platformie kasyna online.85
Recent Comments
Archives
- June 2026
- May 2026
- October 2025
- October 2024
- September 2024
- August 2024
- June 2024
- January 2024
- November 2023
- October 2023
- September 2023
- August 2023
- June 2023
- May 2023
- March 2023
- December 2022
- November 2022
- October 2022
- September 2022
- August 2022
- July 2022
- June 2022
- May 2022
- April 2022
- February 2022
- January 2022
- December 2021
- November 2021
- October 2021
- September 2021
- August 2021
- July 2021
Categories
- 21
- Angular
- AWS
- Backend Development
- Big Data
- Blog
- Cloud
- crypto 20
- crypto 20.05
- crypto 21.05
- Database
- Deployment
- DevOps
- Docker
- fi
- Frontend Development
- GitHub
- Google Cloud Platform
- hu
- Installations
- it
- Java
- JavaScript
- Linux
- MySQL
- Networking
- News
- nl
- no
- NodeJS
- Online casino
- Operating System
- Post
- Python
- Python Flask
- Report
- Security
- Server
- SpringBoot
- Subdomain
- TypeScript
- Uncategorized
- VSCode
- Webhosting
- WordPress
Search
Recent Post
Haaienjacht op de rollen de spanning van
- 3 June, 2026
- 8 min read
Překvapivé příležitosti a herní svět goldzino –
- 3 June, 2026
- 7 min read
Fast Payout Casinos Ireland.2848
- 3 June, 2026
- 7 min read