gpt4 book ai didi

javascript - Internet Explorer 中的数组长度因尾随逗号而异

转载 作者:可可西里 更新时间:2023-11-01 01:28:57 25 4
gpt4 key购买 nike

我目前正在使用数组形式的 Javascript 处理一些数据。该数组末尾可能包含一个空条目,例如 [1,2,]。在 Google Chrome 和 Firefox 中,该示例的长度为 2;但是,在 IE 中,长度为 3。

简而言之:Internet Explorer 为 Javascript 数组提供的长度不同于 Google Chrome 和 Firefox。有没有办法在所有浏览器中对这种行为进行标准化?

代码:

var a = [1,];
alert(a.length);

编辑:

很多答案都说不要尾随逗号,然而,数据是这样给我的。

最佳答案

NEVER have trailing commas in IE. Period.

That goes for ARRAYs too

Javascript Browser Quirks - array.Length

处理您的编辑工作(在 IE8 中测试):

if (a[a.length-1]==null) a.length--;//或 a.pop()

为了更安全的测试,请查看此页面上的其他建议:Length of Array Differs In Internet Explorer With Trailing Comma - DEMO HERE

顺便说一下,没听过elision这句话或 elided之前 - 每天在这里学习新东西

关于javascript - Internet Explorer 中的数组长度因尾随逗号而异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6541163/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com