Node.JS/Javascript - 将字符串转换为整数会在我不希望它返回 NaN 时返回

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11120947/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 04:33:55  来源:igfitidea点击:

Node.JS/Javascript - casting string to integer is returning NaN when I wouldn't expect it to

javascriptstringnode.jscasting

提问by PinkElephantsOnParade

This is all in the context of a larger program, so Im going to try keep it simple, showing the offending lines only. I have an array of values that are numbers in string form a la "84", "32", etc.

这都是在一个更大的程序的上下文中,所以我会尽量保持简单,只显示有问题的行。我有一个值数组,这些值是字符串形式的数字,例如“84”、“32”等。

Yet THIS line

然而这条线

console.log(unsolved.length + " " + unsolved[0] + " " + parseInt(unsolved[0]) + " " + parseInt("84"));

prints:

印刷:

4 "84" NaN 84

"84" is the array element Im trying to parseInt! Yet it won't work unless I take it out of the context of an array and have it explicitly written. What's going on?

“84”是我试图解析的数组元素!然而,除非我把它从数组的上下文中取出并明确地写出来,否则它不会工作。这是怎么回事?

采纳答案by Alex W

You can try removing the quotations from the string to be processed using this function:

您可以尝试使用此函数从要处理的字符串中删除引号:

function stripAlphaChars(source) { 
  var out = source.replace(/[^0-9]/g, ''); 

  return out; 
}

Also you should explicitly specify that you want to parse a base 10 number:

此外,您应该明确指定要解析以 10 为基数的数字:

parseInt(unsolved[0], 10);

回答by lanzz

parseIntwould take everything from the start of its argument that looks like a number, and disregard the rest. In your case, the argument you're calling it with starts with ", so nothing looks like a number, and it tries to cast an empty string, which is really not a number.

parseInt将从它的参数开始处取所有看起来像数字的东西,而忽略其余部分。在你的情况下,你调用它的参数以 开头",所以没有什么看起来像一个数字,它试图转换一个空字符串,它实际上不是一个数字。

回答by Saebekassebil

You should make sure that the array element is indeed a string which is possible to parse to a number. Your array element doesn't contain the value '84', but actually the value '"84"'(a string containing a number encapsulated by ")

您应该确保数组元素确实是一个可以解析为数字的字符串。您的数组元素不包含 value '84',但实际上包含 value '"84"'(包含由 " 封装的数字的字符串)

You'll want to remove the "from your array elements, possible like this:

您需要"从数组元素中删除,可能如下所示:

function removeQuotationMarks(string) {
  return (typeof string === 'string') ? string.replace(/"|'/g, '') : string;
}

unsolved = unsolved.map(removeQuotationMarks);

Now all the array elements should be ready to be parsed with parseInt(unsolved[x], 10)

现在所有的数组元素都应该准备好被解析了 parseInt(unsolved[x], 10)

回答by KARTHIKEYAN.A

First we need to replace " to ' in give data using Regex and replace and then we need to cast.

首先,我们需要使用正则表达式和替换在给定数据中将 " 替换为 ' ,然后我们需要进行转换。

var i = 1;
var j = "22"
function stringToNumber(n) {
  return (typeof n === 'string') ? parseInt(Number(n.replace(/"|'/g, ''))) : n;
}

console.log(stringToNumber(i)); // 1
console.log(stringToNumber(j)); // 22