由于某种原因,for 循环中的字符串增量串联忽略“0”
String incremental concatenation in a for-loop ignores "0" for some reason
我正在编写一个从字符串中获取数字字符串的函数,其工作方式如下:
- 对于字符串中的每个字符,检查它是否是从
0
到 9
的数字
- 如果是,将其包含在新字符串中
- 一直持续到字符串末尾。如果字符串是
a0b109mmn5
,函数应该 return 01095
.
function stringToDigit(string) {
if (string.length == 1) {
switch (string) {
case "0":
return 0;
case "0":
return 0;
case "1":
return 1;
case "1":
return 1;
case "2":
return 2;
case "2":
return 2;
case "3":
return 3;
case "3":
return 3;
case "4":
return 4;
case "4":
return 4;
case "5":
return 5;
case "5":
return 5;
case "6":
return 6;
case "6":
return 6;
case "7":
return 7;
case "7":
return 7;
case "8":
return 8;
case "8":
return 8;
case "9":
return 9;
case "9":
return 9;
}
}
}
function stringToNum(string) {
var numString = "";
for (var i = 0; i < string.length; i++) {
if (stringToDigit(string[i])) numString += string[i];
}
return numString;
}
// $.writeln(stringToDigit("0"));
// $.writeln(stringToNum("000"));
console.log(stringToDigit("0")); // gets 0, good
console.log(stringToDigit("00")); // gets undefined, good
console.log(stringToDigit("a")); // gets undefined, good
console.log(stringToNum("a000b1c2d3e4f5g6h7i8j9")); // gets 123456789, not 000123456789, NOT GOOD!
但是,由于某些原因,我无法在结果中包含“0”。我已经尝试了 Visual Studio 代码(带有一个我忘了名字的控制台插件)和 Adobe ExtendScript Toolkit CC,所以我想知道这是否就是 JavaScript 的工作原理。
if (stringToDigit(string[i]))
- 类似于 if(0)
,其中 0
转换为 false
。请改用 if (stringToDigit(string[i]) !== null)
。
if
语句默认使用 ==
(不是 ===
)。因此我们有一些转换,例如:
null
到 false
,以及
0
到 false
.
请注意,在某些情况下,您应该查看 undefined
。在你的情况下没有默认值,所以要么添加 null
作为默认值,要么选中未定义的:if (stringToDigit(string[i]) !== undefined)
虽然 Alex 完美地回答了你的问题,但我有几分钟的时间对如果我必须完成你的任务我会使用的东西进行了一些重构。
function stringToNum(str) {
// Object with all the string numbers and their digit equivalent
var replacements = {
"0": 0,
"0": 0,
"1": 1,
"1": 1,
"2": 2,
"2": 2,
"3": 3,
"3": 3,
"4": 4,
"4": 4,
"5": 5,
"5": 5,
"6": 6,
"6": 6,
"7": 7,
"7": 7,
"8": 8,
"8": 8,
"9": 9,
"9": 9,
};
var re = new RegExp(Object.keys(replacements).join("|"),"gi");
// Do all the replacements on characters matching object keys
return str.replace(re, function(matched){
return replacements[matched];
})
// Remove all the characters that are not a digit
.replace(/\D+/g, '');
}
console.log(stringToNum("a000b1c2d3e4f5g6h7i8j9"));
这种方式应该更快并且避免错误的 0 陷阱。
这里是jsbin
我正在编写一个从字符串中获取数字字符串的函数,其工作方式如下:
- 对于字符串中的每个字符,检查它是否是从
0
到9
的数字
- 如果是,将其包含在新字符串中
- 一直持续到字符串末尾。如果字符串是
a0b109mmn5
,函数应该 return01095
.
function stringToDigit(string) {
if (string.length == 1) {
switch (string) {
case "0":
return 0;
case "0":
return 0;
case "1":
return 1;
case "1":
return 1;
case "2":
return 2;
case "2":
return 2;
case "3":
return 3;
case "3":
return 3;
case "4":
return 4;
case "4":
return 4;
case "5":
return 5;
case "5":
return 5;
case "6":
return 6;
case "6":
return 6;
case "7":
return 7;
case "7":
return 7;
case "8":
return 8;
case "8":
return 8;
case "9":
return 9;
case "9":
return 9;
}
}
}
function stringToNum(string) {
var numString = "";
for (var i = 0; i < string.length; i++) {
if (stringToDigit(string[i])) numString += string[i];
}
return numString;
}
// $.writeln(stringToDigit("0"));
// $.writeln(stringToNum("000"));
console.log(stringToDigit("0")); // gets 0, good
console.log(stringToDigit("00")); // gets undefined, good
console.log(stringToDigit("a")); // gets undefined, good
console.log(stringToNum("a000b1c2d3e4f5g6h7i8j9")); // gets 123456789, not 000123456789, NOT GOOD!
但是,由于某些原因,我无法在结果中包含“0”。我已经尝试了 Visual Studio 代码(带有一个我忘了名字的控制台插件)和 Adobe ExtendScript Toolkit CC,所以我想知道这是否就是 JavaScript 的工作原理。
if (stringToDigit(string[i]))
- 类似于 if(0)
,其中 0
转换为 false
。请改用 if (stringToDigit(string[i]) !== null)
。
if
语句默认使用 ==
(不是 ===
)。因此我们有一些转换,例如:
null
到false
,以及0
到false
.
请注意,在某些情况下,您应该查看 undefined
。在你的情况下没有默认值,所以要么添加 null
作为默认值,要么选中未定义的:if (stringToDigit(string[i]) !== undefined)
虽然 Alex 完美地回答了你的问题,但我有几分钟的时间对如果我必须完成你的任务我会使用的东西进行了一些重构。
function stringToNum(str) {
// Object with all the string numbers and their digit equivalent
var replacements = {
"0": 0,
"0": 0,
"1": 1,
"1": 1,
"2": 2,
"2": 2,
"3": 3,
"3": 3,
"4": 4,
"4": 4,
"5": 5,
"5": 5,
"6": 6,
"6": 6,
"7": 7,
"7": 7,
"8": 8,
"8": 8,
"9": 9,
"9": 9,
};
var re = new RegExp(Object.keys(replacements).join("|"),"gi");
// Do all the replacements on characters matching object keys
return str.replace(re, function(matched){
return replacements[matched];
})
// Remove all the characters that are not a digit
.replace(/\D+/g, '');
}
console.log(stringToNum("a000b1c2d3e4f5g6h7i8j9"));
这种方式应该更快并且避免错误的 0 陷阱。
这里是jsbin