i've declared an array:
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' },
];
When trying to then iterate over every member of the array:
for (var a = 0; a < employeeBanks.length; a++)
{
...
}
i get an error, because there is an extra item in my array:
employeeBanks
[0] = [object]
[1] = [object]
[2] = [object]
[3] = [object]
[prototype] = [object]
And the 5th element in the array is nothing recognizable. So i get an error. Running IE11 in IE9, IE10, or edge mode, the prototype
member does not appear (or does not appear when iterating an array).
What is the recommended technique to only iterate members of an array? Are there any other expected things that might appear in an array without my permission?
Bonus Screenshot:
Full source:
<!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>
Remove <meta http-equiv="X-UA-Compatible" content="IE=8" />
, and it works.
Your extra element is due to the extra comma. Remove the comma before the closing ]
and you will have the correct number of elements in your array.
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' }
];