Categories
JavaScript Answers

How to replace a string in a JavaScript array?

Spread the love

To replace a string in a JavaScript array, we use the array map and string replaace methods.

For instance, we write

const resultArr = arr.map((x) => {
  return x.replace(/,/g, "");
});

to call arr.map with a callback that returns a new string that replace commas in x with empty strings with replace.

A new array with the replacements done is returned.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *