1

我已经声明了一个数组:

var employeeBanks = 
[
    { validFromDate: '2013-01-01',    validToDate: '2013-12-31' },
    { validFromDate: '2013-01-01',    validToDate: '2013-12-31' },
    { validFromDate: '2013-01-01',    validToDate: '2013-12-31' },
    { validFromDate: '2013-02-01',    validToDate: '2014-01-31' },
];

当尝试然后迭代数组的每个成员时:

for (var a = 0; a < employeeBanks.length; a++)
{
   ...
}

我收到一个错误,因为我的数组中有一个额外的项目:

employeeBanks
   [0] = [object]
   [1] = [object]
   [2] = [object]
   [3] = [object]
   [prototype] = [object]

数组中的第 5 个元素无法识别。所以我得到一个错误。在 IE9、IE10 或边缘模式下运行 IE11,prototype成员不出现(或在迭代数组时不出现)。

仅迭代数组成员的推荐技术是什么?未经我的许可,是否有任何其他预期的东西可能出现在数组中?

奖励截图:

在此处输入图像描述

完整来源:

<!doctype html>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=8" />
<script type="text/javascript">
    var employeeBanks = 
        [
            { validFromDate: '2013-01-01',    validToDate: '2013-12-31' },
            { validFromDate: '2013-01-01',    validToDate: '2013-12-31' },
            { validFromDate: '2013-01-01',    validToDate: '2013-12-31' },
            { validFromDate: '2013-02-01',    validToDate: '2014-01-31' },
        ];
</script>
<body>
<p id="lblFoo">

<script type="text/javascript">
    for (var a = 0; a < employeeBanks.length; a++)
    {
       document.getElementById("lblFoo").innerText = "Test failed";
       document.getElementById("lblFoo").innerText = employeeBanks[a].validToDate;
    }
   document.getElementById("lblFoo").innerText = "Test complete";
</script>
</body>
</html>

删除<meta http-equiv="X-UA-Compatible" content="IE=8" />,它的工作原理。

也可以看看

4

1 回答 1

1

您的额外元素是由于额外的逗号。在关闭之前删除逗号],您将在数组中拥有正确数量的元素。

var employeeBanks = 
[
    { validFromDate: '2013-01-01',    validToDate: '2013-12-31' },
    { validFromDate: '2013-01-01',    validToDate: '2013-12-31' },
    { validFromDate: '2013-01-01',    validToDate: '2013-12-31' },
    { validFromDate: '2013-02-01',    validToDate: '2014-01-31' }
];
于 2013-11-21T17:07:23.350 回答