Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Array how to convert string to numbers except the operators

I have tried to convert the string type to numbers in array but the operators are in the way.

let outputArray = ['3', '5', '7' ,'+', '*', '9', '-' ];
numoutputArray = outputArray.map(Number);
console.log(numoutputArray)
//[ 3, 5, 7, NaN, NaN, 9, NaN ]

I wanted to get the array as [3,5,7,'+','*',9,'-'].

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

this way…

let outputArray = ['3', '5', '7' ,'+', '*', '9', '-' ]

numoutputArray = outputArray.map(v=>isNaN(v)?v:Number(v))

console.log( JSON.stringify( numoutputArray ))
 
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading