数组的长度不同在Internet Explorer尾随逗号 [英] Length of Array Differs In Internet Explorer With Trailing Comma
问题描述
我目前正在使用Javascript是在数组形式的一些数据的工作。该阵列可以包含在最后一个空项,如 [1,2]
。在谷歌Chrome和Firefox,这个例子的长度为2;然而,在IE中,长度为3
I'm currently working with some data using Javascript that is in the form of an array. The array may contain an empty entry at the end, such as [1,2,]
. In Google Chrome and Firefox, the length of that example would be 2; however, in IE, the length is 3.
在短期:Internet Explorer被赋予不同的长度在Javascript数组比谷歌Chrome和Firefox。有没有一种办法,以规范在所有浏览器这种行为?
In short: Internet Explorer is giving a different length for an array in Javascript than Google Chrome and Firefox. Is there a way to standardize this behavior across all browsers?
code:
var a = [1,];
alert(a.length);
编辑:
很多答案都在说不要有后面的逗号,但是,这些数据是给我这个样子。
A lot of answers are saying not to have a trailing comma, however, the data is given to me in this way.
推荐答案
浏览器的Javascript怪癖 - array.Length
要处理你的编辑工作原理(在IE8测试中):
To handle your edit this works (tested in in IE8):
如果(A [则为a.length-1] == NULL)a.length--; //或a.pop()
有关更安全测试,请看看这个页面上的其他建议:<一href=\"http://stackoverflow.com/questions/6541163/javascript-length-of-array-in-internet-explorer/6541189#6541189\">Javascript:在Internet Explorer 阵列的长度 - DEMO这里
For a safer test, please look at the other suggestion on this page: Javascript: Length of Array in Internet Explorer - DEMO HERE
顺便说一句,从来没有听说过的话省音或的elided~~V - 学习新的东西每天在这里
By the way, never heard the words elision or elided before - learn something new here every day
这篇关于数组的长度不同在Internet Explorer尾随逗号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!