JS将分隔符分隔的字符串转换为数组元素
JS convert delimiter separated string to array elements
为了好玩,我写了一个快速脚本,用逗号分隔字符串,用逗号表示不同的单词,并输出一个以单词为元素的数组。我的问题在于我的逻辑——这只适用于原始字符串中的逗号。
实际情况是,该算法只查看我在调用函数时提供的分隔符数组中的第一个元素(逗号)。我正在考虑在某个地方使用布尔值作为标志,但不确定这是否真的是最好的方法。有什么建议吗?
var stringToArray = function(delimiterArray, originalString) {
var arrayOutput = []
var tempWord = ""
for (var i = 0; i < originalString.length; i++) {
for (var j = 0; j < delimiterArray.length; j++) {
var currentCharacter = originalString.charAt(i)
var currentDelimiter = delimiterArray[j]
while (currentCharacter != currentDelimiter) {
tempWord += currentCharacter
//once we hit a delimiter, break so that we can move onto the next conditional statement.
break
}
if (currentCharacter === currentDelimiter) {
//push word onto the array to hold each string, and then break out so we can go back to the iteration of the nested loops
arrayOutput.push(tempWord)
tempWord = ""
break
}
//we break out of the second for loop ->
break
}
}
return arrayOutput
}
delims = [',', '.', ';', ' ']
originalString = "USA,Canada,Mexico,Bermuda,Grenada,Belize"
finalOutput = stringToArray(delims, originalString)
console.log(finalOutput)
如果我使用"."作为我用作参数的字符串中的分隔符,我的算法失败了。我在C中使用布尔标志(很久以前)实现了这一点,因为我没有使用任何内置函数,所以我必须编写更多的代码(必须找到所有函数的长度,这样我才能为数据结构存储足够的内存)。不过,我不想重新访问那个旧代码。
问题是,如果当前字符与当前分隔符不匹配,则将其添加到tempWord
中。因此,如果当前字符与第一个分隔符不匹配,它将被添加到tempWord中,即使它可能与其他分隔符之一匹配。您需要遍历整个分隔符数组,然后才能确定该字符是否为分隔符。
var stringToArray = function(delimiterArray, originalString) {
var arrayOutput = [];
var tempWord = "";
for (var i = 0; i < originalString.length; i++) {
var currentCharacter = originalString.charAt(i)
var isDelimiter = false;
for (var j = 0; j < delimiterArray.length; j++) {
var currentDelimiter = delimiterArray[j]
if (currentCharacter == currentDelimiter) {
isDelimiter = true;
//once we hit a delimiter, break so that we can move onto the next conditional statement.
break;
}
}
if (isDelimiter) {
arrayOutput.push(tempWord);
tempWord = "";
} else {
tempWord += currentCharacter;
}
}
return arrayOutput;
}
delims = [',', '.', ';', ' ']
originalString = "USA,Canada.Mexico,Bermuda,Grenada,Belize"
finalOutput = stringToArray(delims, originalString)
console.log(finalOutput)
出于某种原因,这似乎是人们编写自己的数组搜索代码时非常常见的错误。
这是一个经过更正的脚本,注释了更改:
var stringToArray = function(delimiterArray, originalString) {
var arrayOutput = []
var tempWord = ""
for (var i = 0; i < originalString.length; i++) {
// Set currentCharacter here, as it does not depend on j:
var currentCharacter = originalString.charAt(i)
for (var j = 0; j < delimiterArray.length; j++) {
var currentDelimiter = delimiterArray[j]
if (currentCharacter === currentDelimiter) {
//push word onto the array to hold each string, and then break out so we can go back to the iteration of the nested loops
arrayOutput.push(tempWord)
tempWord = ""
break
}
//No, we don't break out of the second for loop ->
}
// Moved out of the j-loop, and turned into an IF without BREAK:
if (currentCharacter != currentDelimiter) {
tempWord += currentCharacter
}
}
return arrayOutput
}
var delims = [',', '.', ';', ' ']
var originalString = "USA,Canada,Mexico,Bermuda,Grenada,Belize"
var finalOutput = stringToArray(delims, originalString)
document.write(JSON.stringify(finalOutput))
您遇到了几个问题:
- 你在while循环中使用了一个break。这什么也没做,它突破了while循环,而不是上循环
- for循环结束时的中断没有任何作用,它中断了for循环
- 你错过了最后一个单词,你需要把它推到最后的数组上
- 我已经对
isDeliminator()
函数进行了除法器检查。这使得代码更加清晰、模块化(您可以再次使用它),并且通常可以减少错误
下面是我的代码。
function stringToArray(delimiterArray, originalString) {
var arrayOutput = [];
var tempWord = '';
for (var i = 0; i < originalString.length; i++) {
var currentCharacter = originalString.charAt(i);
if (isDeliminator(currentCharacter)) {
//push word onto the array to hold each string, and then break out so we can go back to the iteration of the nested loops
arrayOutput.push(tempWord);
tempWord = '';
} else {
tempWord += currentCharacter;
}
}
// Push the last work onto the array
arrayOutput.push(tempWord);
return arrayOutput;
}
function isDeliminator(char) {
for (var i = 0; i < delims.length; i += 1) {
if (char === delims[i]) {
return true;
}
}
return false;
}
var delims = [',', '.', ';', ' '];
var inputString = 'USA.Canada,Mexico;Bermuda,Grenada,Belize';
var finalOutput = stringToArray(delims, inputString);
console.log(finalOutput);
相关文章:
- 在函数中添加数组元素的数值
- 访问JSON对象内部的数组元素
- Mongoose-在更新中删除数组元素
- javascript数组元素是否知道其封闭数组
- 将数组元素附加到FormData dos'不适用于Firefox 15
- 如何在javascript中使用click函数选择数组元素
- 如何在JavaScript中剥离数组元素中的非整数
- 消隐数组元素是否生成自己的属性
- 如何使一个Math.random数组元素比另一个数组元素更有可能被选中
- 在Codrops的内容中添加数组元素展开缩略图网格预览
- 如何使用Jquery水平打印表中的数组元素,并在某个元素之后垂直打印
- 如何访问数组中的数组元素(JavaScript)
- 生成ACF标记位置的数组(元素列表后缺少])
- validate.js验证数组元素
- JavaScript Unshift EACH 数组元素
- JS将分隔符分隔的字符串转换为数组元素
- 如何在加号运算符之后选择数组元素的一部分
- 访问标记图标的图像数组元素
- JavaScript 合并相同的数组元素
- 将文件转换为数组并使用数据列表和选项标签为自动完成文本框添加数组元素