JavaScript Answers

How to split and modify a string in Node?

Spread the love

To split and modify a string in Node, we call the split and map methods.

For instance, we write

const str = "123, 124, 234,252";
const arr = str.split(",").map((val) => Number(val) + 1);

to call split to split the str string by the commas into a string array.

And then we call map to map each value to their new value by converting them to a number and add 1 to it.

By John Au-Yeung

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

Leave a Reply

Your email address will not be published.