I have an array defined as:
var subjectCache = [];
I then have some code to build it up, which is working ok.
However, if I try to reference the array by an index, e.g.:
var x = subjectCache[0];
or
var x = subjectCache[1];
I get undefined
.
Also subjectCache.length
is always 0 (zero).
if I try to reference it by its key, e.g.:
var x = subjectCache['12345'];
it works.
Is this normal? Shouldn't I be able to reference it by its index whatever?
I'm using Internet Explorer, if it makes a difference (and it probably does :( )
[Edit]
this is the code I'm using to build the array, although I really don't think it is to blame.
It's a callback from a webservice call. This is working fine and the array is being populated.
var subjectCache = [];
var subjectCacheCount = 0;
function refreshSubjectsCallback(data) {
// update subjects
// loop through retrieved subjects and add to cache
for( i=0; i < data.length; i++ )
{
var subject = data[i];
var subjectid = subject.SubjectId;
subjectCache[subjectid] = subject;
subjectCacheCount += 1;
}
}
[/Edit]
I have an array defined as:
var subjectCache = [];
I then have some code to build it up, which is working ok.
However, if I try to reference the array by an index, e.g.:
var x = subjectCache[0];
or
var x = subjectCache[1];
I get undefined
.
Also subjectCache.length
is always 0 (zero).
if I try to reference it by its key, e.g.:
var x = subjectCache['12345'];
it works.
Is this normal? Shouldn't I be able to reference it by its index whatever?
I'm using Internet Explorer, if it makes a difference (and it probably does :( )
[Edit]
this is the code I'm using to build the array, although I really don't think it is to blame.
It's a callback from a webservice call. This is working fine and the array is being populated.
var subjectCache = [];
var subjectCacheCount = 0;
function refreshSubjectsCallback(data) {
// update subjects
// loop through retrieved subjects and add to cache
for( i=0; i < data.length; i++ )
{
var subject = data[i];
var subjectid = subject.SubjectId;
subjectCache[subjectid] = subject;
subjectCacheCount += 1;
}
}
[/Edit]
Share Improve this question edited Oct 3, 2012 at 15:28 CompanyDroneFromSector7G asked Oct 3, 2012 at 15:22 CompanyDroneFromSector7GCompanyDroneFromSector7G 4,53715 gold badges61 silver badges101 bronze badges 10- What code do you use to build your array up? – Blender Commented Oct 3, 2012 at 15:23
- Can you post some code that shows how you're adding items to the array? What version of internet explorer? And have you tested on any other browser? – Ian Commented Oct 3, 2012 at 15:23
- I ensure you, this time it has nothing to do with IE. – jAndy Commented Oct 3, 2012 at 15:23
-
It's not possible for an array to have a property with key
12345
and a.length
of zero. Please post a demo on e.g. jsFiddle. – pimvdb Commented Oct 3, 2012 at 15:24 - Seems like you are using non-numerical keys for arrays. Don't do that, use objects instead arrays then. Arrays only really work with numerical keys. – Felix Kling Commented Oct 3, 2012 at 15:24
5 Answers
Reset to default 3You're probably assigning keys manually instead of using subjectCache.push()
to add new elements to the array:
var array = [];
array['foo'] = 'bar';
console.log(array.length); // 0
The length
attribute isn't going to reflect those changes the way you'd expect:
> var a = [];
undefined
> a[100] = 2; // The previous `100` entries evaluate to `undefined`
2
> a.length;
101
Instead, use an object:
var object = {};
object['foo'] = 'bar';
for (var key in object) {
var value = object[key];
console.log(value);
}
From your symptoms, it sounds like you are trying to treat the array as an associative array.
In Javascript, arrays work like this:
var a = [];
a[1] = 10;
alert(a.length);
Objects work like this:
var o = {};
o.myProp = true;
o["myOtherProp"] = false;
Arrays only work with numeric keys not strings. Strings assign properties to the object, and aren't counted as part of length
nor it's numeric indices.
When building the array, make sure you are assigning to a numeric position within the array.
No, it will not work, because you haven't created arrays but objects. you will have to access it by its key.
var x = subjectCache['12345'];
If this works and subjectCache.length
doesn't, I think you are making an object not an array. You are confused.
Somewhere along the road you lost the array, and the variable subjectCache
points to a different kind of object.
If it was an array, it can't have the length zero and contain an item that is reachable using subjectCache['12345']
. When you access an item in an array it doesn't make any difference if you use a numeric index or a string representing a number.