-1

I have a parent array containing child-arrays. length property on parent array displays 0 results, even when there are child arrays present.

Here's the code:

var balances = [];
balances['kanav'] = 50;
balances['yash'] = 50;

alert(balances.length);
console.log(balances);

What's the reason?

RobG
  • 142,382
  • 31
  • 172
  • 209
Kanav
  • 2,695
  • 8
  • 34
  • 56
  • 1
    Please add the relevant [mcve] code to your question; don't just link to an e thermal resource. – David Thomas Oct 10 '15 at 11:07
  • The `length` property is defined as the highest index plus 1, or 0 if it doesn’t contain any indices. Those properties do not count as indices. – Sebastian Simon Oct 10 '15 at 11:07
  • 1
    @Xufox—not exactly, *length* is **at least** the highest index plus one. It can be higher (e.g. `var a = new Array(8)`. – RobG Oct 10 '15 at 11:09
  • Why so many down-votes? Explaining problem with a JSFiddle example. Did i do anything wrong? – Kanav Oct 10 '15 at 11:12
  • It looks like an array, but it's an object! Good explanation here: http://stackoverflow.com/questions/2528680/javascript-array-length-incorrect-on-array-of-objects – MBaas Oct 10 '15 at 11:12

4 Answers4

6

The length property only works for Array properties that are numeric values. Arrays are just Objects with a special length property and some handy methods inherited from Array.prototype.

When you add properties like kanav and yash, they are added as plain object properties, not indexes, therefore don't affect length.

var arr = [];

// Add plain object properties
arr.foo = 'foo';
arr.bar = 'bar';

document.write(arr.length) // 0

// Add some numeric properties
// Since they are numbers, square bracket notation must be used
arr[9] = 9;

document.write('<br>' + arr.length) // 10
RobG
  • 142,382
  • 31
  • 172
  • 209
1

In your example balances is not an array, it is actually an associative array (they're both objects, but they have different methods).

If you want to use an array you could

var balances = [
    ["kanav", 50],
    ["yash", 50]
];
alert(balances.length);
console.log(balances);

If you wanted to stay with your current format you would have to loop over the objects to count them

var balances = [];
balances['kanav'] = 50;
balances['yash'] = 50;

var i=0;
for(var key in balances) {
    if(balances.hasOwnProperty(key)) {
        i++;
    }
}
console.log(i);
Fletch
  • 450
  • 1
  • 4
  • 15
1

You are using the array as a json object, but an array handle indexes or push functions. Therefor if you use it in this way, it will produce the right result.

var balances = [];
balances.push('something');
balances.push('another');

alert(balances.length);
console.log(balances);

Or otherwise in this syntax:

var balances = [];
balances[0] = 50;
balances[1] = 50;

alert(balances.length);
console.log(balances);
mrhn
  • 17,961
  • 4
  • 27
  • 46
0

balances is an associative array. According to this post, you can obtain its length, in this way:

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

var balances = [];
balances['kanav'] = 50;
balances['yash'] = 50;

alert(Object.size(balances));
console.log(balances);
Community
  • 1
  • 1
user3406222
  • 309
  • 2
  • 11