How to split and modify a string in NodeJS?

0 votes

I have a string :

var str = "123, 124, 234,252";

I want to parse each item after split and increment 1. So I will have:

var arr = [124, 125, 235, 253 ];

How can I do that in NodeJS?

Oct 16 in Node-js by kartik
• 35,720 points
34 views

1 answer to this question.

0 votes

Hello @kartik,

Use split and map function:

var str = "123, 124, 234,252";
var arr = str.split(",");
arr = arr.map(function (val) { return +val + 1; });

Notice +val - string is casted to a number.

Or shorter:

var str = "123, 124, 234,252";
var arr = str.split(",").map(function (val) { return +val + 1; });

Hope it helps!!

Thank you!!

answered Oct 16 by Niroj
• 77,920 points

Related Questions In Node-js

0 votes
1 answer

How to get GET (query string) variables in Express.js on Node.js?

Hello @kartik, Since you've mentioned Express.js in your ...READ MORE

answered Jul 8 in Node-js by Niroj
• 77,920 points
413 views
0 votes
1 answer

How to print a stack trace in Node.js?

Hello @kartik, To print stacktrace of Error in console in ...READ MORE

answered Jul 9 in Node-js by Niroj
• 77,920 points
65 views
0 votes
1 answer

How to append to a file in Node?

Hii @kartik, For occasional appends, you can use appendFile, ...READ MORE

answered Jul 9 in Node-js by Niroj
• 77,920 points
78 views
0 votes
1 answer

How to use executables from a package installed locally in node_modules?

Hello @kartik, Use the npm bin command to get the ...READ MORE

answered Jul 13 in Node-js by Niroj
• 77,920 points
57 views
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18 in Laravel by Niroj
• 77,920 points
3,500 views
0 votes
1 answer

What is redirection in Laravel?

Named route is used to give specific ...READ MORE

answered Mar 18 in Laravel by Niroj
• 77,920 points
418 views
0 votes
1 answer

How to install Laravel via composer?

Hello, This is simple you just need to ...READ MORE

answered Mar 23 in Laravel by Niroj
• 77,920 points
287 views
+1 vote
1 answer

What are named routes in Laravel and How can specify route names for controller actions?

Hey @kartik, Named routing is another amazing feature of ...READ MORE

answered Mar 23 in Laravel by Niroj
• 77,920 points
4,197 views
0 votes
1 answer

How to update a value in a json file and save it through node.js?

Hello @kartik, It's particularly useful if you're concerned ...READ MORE

answered Oct 11 in Node-js by Niroj
• 77,920 points
693 views
0 votes
1 answer

How to provide a mysql database connection in single file in nodejs?

Hello @kartik, You could create a db wrapper ...READ MORE

answered Oct 15 in Node-js by Niroj
• 77,920 points
74 views