给定以下字符串:
var str = "one,two,three";
如果我按逗号分割字符串,通常会得到一个数组,就像预期的那样:
var arr = str.split(/\s*,\s*/);
问题在于在Google Chrome(Mac版)中,它会将额外的属性附加到数组上。
来自Chrome调试器的输出:
arr: Array
0: one
1: two
2: three
constructor: function Array()
index: undefined
input: undefined
length: 3
如果我使用
for/in
循环迭代数组,它会遍历新属性。具体来说,是input
和index
属性。使用hasOwnProperty
似乎没有帮助。解决方法是基于数组长度进行
for
循环。但我仍然想知道为什么Chrome会这样行事。Firefox和Safari没有这个问题。
str.split(',')
? - Andy Efor/in
不好。收到。仍然好奇为什么 Chrome 会尝试迭代input
和index
。或者它们为什么存在。 - user113716