Mantar
Mantar

Reputation: 2720

How to find length of literal array?

So, why does this result in 0 and how do I find the actual size?

var array = [];
array["foo"] = "bar";
array["bar"] = "foo";

document.write(array.length);

Upvotes: 9

Views: 9328

Answers (7)

Zen
Zen

Reputation: 392

Object.keys(array).length

see this: https://stackoverflow.com/a/13802999/1488217

credits goes to ian

Upvotes: 0

hunter
hunter

Reputation: 63522

First off, the length is 0 because the number of items in array is 0.

When you do this syntax array["foo"] = "bar" you're creating a property called foo that has a value of bar. The length is still 0 since you have not added anything to the Array, you've just set new properties on the Array

Working version here: http://jsfiddle.net/VyjJD/3/

var array = [];

array["foo"] = "bar";
array["bar"] = "foo";
array.bin = function() { return "bin"; };

array[0] = "bar";
array[1] = "foo";
array[2] = "bin";
array[3] = "bat";

var props = 0;

for (key in array)
    props++;

document.write(array.length + "<br />" 
           + props + "<br />" 
           + (array.foo == array["foo"]) + "<br />" 
           + array.bar + "<br />"
           + array.bin());

Notice that array.length = 4 but props = 7 which is all of the properties and the number of items in the Array.

Upvotes: 7

karim79
karim79

Reputation: 342655

Since that is a object, which is comprised of properties, and take the form of key/value pairs (conceptually similar to an associative array, or a hash table) you will have to do something like this:

Object.size = function(obj) {
    var size = 0, key;
    for (key in obj) {
        if (obj.hasOwnProperty(key)) size++;
    }
    return size;
};

var array = [];
array["foo"] = "bar";
array["bar"] = "foo";

var size = Object.size(array);

Demo: http://jsfiddle.net/gBG34/

Upvotes: 6

Jos&#233; Leal
Jos&#233; Leal

Reputation: 8109

basically when you do

array["foo"] = "bar"

it just adds some more attributes to the array, which is an object. In javascript, array.foo and array['foo'] means the same.

Upvotes: 1

lonesomeday
lonesomeday

Reputation: 237905

You are setting a property on the array, not giving it a new element. Arrays can receive arbitrary properties, just like any other Javascript object. For instance:

var foo = [];
foo.bar = 'foobar';
console.log(foo.bar); // outputs 'foobar'
console.log(foo); // outputs [] -- empty array

To add items to an array, use Array.push:

var foo = [];
foo.push('foobar');
console.log(foo); // outputs ['foobar']

If you want key=>value pairs, use an object instead:

var foo = {};
foo['bar'] = 'foobar';
console.log(foo); // outputs {bar: 'foobar'}

Upvotes: 4

Sergey Ilinsky
Sergey Ilinsky

Reputation: 31535

The actual size is 0 because you did not put any items into array object - this can only be done either by a method (push etc.) or by the means of assigning value to an indexed property (or in constructor).

Upvotes: 0

ilivewithian
ilivewithian

Reputation: 19702

Maybe I'm being naive (my javascript isn't what it might be), but shouldn't it be something like this:

var array = [];
array[0] = "bar";
array[1] = "foo";
document.write(array.length);

Upvotes: 0

Related Questions