9

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);

7 Answers 7

7

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.

Sign up to request clarification or add additional context in comments.

Comments

6

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/

9 Comments

Or also: Object.prototype.size = function()... And then you can use it like: var size = array.size()
...or as a plain-old function, and just do getSize(var);
There's no such thing as an associative array in JS. It's an object and just an alternative syntax to access it's properties. Instead of array["foo"] you may as well write array.foo. That's why the length property doesn't work. He's setting properties on an array object and not adding items to an array.
@DanMan Fair enough, but how do I find how many properties the array has then?
@DanMan - But you can think of it as one. In the same way you can call it a 'hash table'. I'm aware that 'properties' can be accessed using both square bracket and dot notation.
|
4

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'}

Comments

1

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.

2 Comments

Thanks for clearing that out. But it doesn't answer the question?
It doesn't convert it into an object. Arrays are objects, so you can set arbitrary properties on them.
0

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);

Comments

0

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).

Comments

0

Object.keys(array).length

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

credits goes to ian

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.