1

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]

10
  • What code do you use to build your array up? 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? Commented Oct 3, 2012 at 15:23
  • I ensure you, this time it has nothing to do with IE. 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. 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. Commented Oct 3, 2012 at 15:24

5 Answers 5

3

You'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);
}
Sign up to request clarification or add additional context in comments.

Comments

1

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;

Comments

1

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.

2 Comments

This is not correct here I'm afraid. arr[12345] is the same as arr['12345'].
Numeric strings are considered though (because all property names are internally stored as strings anyway).
1

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.

1 Comment

[] does create an array, and arr['12345'] works the same way as arr[12345].
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.