Javascript .trim() 不是函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32991269/
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
.trim() is not a function
提问by gardni
I have a very simple string that can contains a list which can possibly contain whitespace:
我有一个非常简单的字符串,它可以包含一个可能包含空格的列表:
string = "one, two,three ";
I want to first split the string by ,
to create an array of three strings and then remove any whitespace using .trim()
我想首先拆分字符串,
以创建一个包含三个字符串的数组,然后使用删除任何空格.trim()
array = string.split(',').trim();
which returns "one","two","three"
返回 "one","two","three"
however sometimes it fails and returns an error .trim() is not a function
但是有时它会失败并返回错误 .trim() is not a function
I read that .trim()
returns a new string not a trimmed version of the current string. So i used a for loop to do the above:
我读到.trim()
返回一个新字符串,而不是当前字符串的修剪版本。所以我用了一个 for 循环来做上面的事情:
array = string.split(',');
for (var i = 0; i < array.length; i++) {
var item = array[i].trim();
array.push(item);
}
which returns "one","two","three"
返回 "one","two","three"
my question is, can anyone explain why i was getting the error only sometimes? if the array never changed from my example and can anyone provide a cleaner solution to my fix.
我的问题是,谁能解释为什么我只是有时会收到错误?如果数组从未从我的示例中改变,并且任何人都可以为我的修复提供更清晰的解决方案。
回答by InsOp
split()
does not work on arrays, but on strings.
As you wrote your String into an array you have to get that string via array[0]
, since the string is the very first Element.
split()
不适用于数组,但适用于字符串。当您将 String 写入数组时,您必须通过 获取该字符串array[0]
,因为该字符串是第一个元素。
If you splitted your string into an array you can call the map function and trim each value in your newly created array. like this:
如果将字符串拆分为数组,则可以调用 map 函数并修剪新创建的数组中的每个值。像这样:
array = ["one, two,three "];
array = array[0].split(',')
array = array.map(function(a){return a.trim()})
Or in short:
或者简而言之:
array = ["one, two,three "][0].split(',').map(function(a){return a.trim()})
回答by Roberto
In ES6 compliant runtime (i.e: node 4+) you can use use arrow function that gives you a less verbose code to achieve your goal:
在符合 ES6 的运行时(即:节点 4+)中,您可以使用箭头函数,为您提供更简洁的代码来实现您的目标:
> "one, two, three ".split(',').map(s => s.trim());
[ 'one', 'two', 'three' ]
回答by Tom Fennelly
I saw this same issue trying to iterate the output of String.split using a "for (var i in splitTokens)". Note it only happened to me in the browser (Chrome in my case) i.e. not when using the same code in a node process.
我看到了同样的问题,试图使用“for (var i in splitTokens)”来迭代 String.split 的输出。请注意,它只发生在我的浏览器中(在我的例子中是 Chrome),即在节点进程中使用相同的代码时不会发生。
It confused me until I hooked up a debugger and then I saw the problem. String.split returns an Array object instance (splitTokens), which has functions on it ("each" etc). The "for (var i in splitTokens)" loop iterates the functions too, so splitTokens[i] was getting back e.g. the each function of the Array instance, which caused things to explode if I tried to call e.g. "trim()" (because the function object doesn't have a function called trim()).
这让我很困惑,直到我连接了一个调试器,然后我看到了这个问题。String.split 返回一个 Array 对象实例 (splitTokens),其中包含函数(“each”等)。“for (var i in splitTokens)”循环也迭代函数,所以 splitTokens[i] 正在返回例如 Array 实例的每个函数,如果我尝试调用例如“trim()”,这会导致事情爆炸(因为函数对象没有名为 trim() 的函数)。