简体   繁体   English

无法将字符串转换为数字

[英]unable to convert string to number

I am trying to convert string to number but I am unable to do that , after passing each number from foreach 我正在尝试将字符串转换为数字,但是从foreach传递每个数字后,我无法做到这一点

function sum(num){

 num.toString().split('').forEach(add);

}


function add(value , key) {
    console.log(value);    //  2,3 
   Number(value);
   console.log(typeof(value))   // string string

}


sum(23);

In the code - > 在代码中->

After I did Number(value); 我做完Number(value); in function add(value, key) I am still getting string console.log(typeof(value)) // string 在函数add(value,key)中我仍然得到字符串console.log(typeof(value))//字符串

You're not assigning it to a new variable / reassigning the variable: 您没有将其分配给新变量/未重新分配该变量:

 function sum(num){ num.toString().split('').forEach(add); } function add(value , key) { console.log( { value } ); const newValue = Number(value); // or alternatively value = Number(value); console.log( typeof newValue ); } sum(23); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM