How to use map() Method on Arrays In Javascript
We can convert the string into an array of characters, use the map() method to iterate over each character, and replace dots with spaces. Finally, we join the array back into a string.
Example: In this example, we replace dots (β.β) with spaces (β β) using the map() method.
// Assigning a string
let str = "Learning.is.fun.with.JavaScript";
// Using map() to replace dots with spaces
let newStr = Array.from(str).map(char => char === '.' ? ' ' : char).join('');
// Printing original string
console.log("String 1: " + str);
// Printing replaced string
console.log("String 2: " + newStr);
Output
String 1: Learning.is.fun.with.JavaScript String 2: Learning is fun with JavaScript
How to replace all dots in a string using JavaScript ?
We will replace all dots in a string using JavaScript. There are multiple approaches to manipulating a string in JavaScript.
Table of Content
- Using JavaScript replace() Method
- Using JavaScript Split() and Join() Method
- Using JavaSccript reduce() Method and spread operator
- Using JavaScript replaceAll() Method
- Using JavaScript for loop
- Using JavaScript map() Method on Arrays