0

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?

5
  • 1
    Please add the relevant minimal reproducible example code to your question; don't just link to an e thermal resource. Commented Oct 10, 2015 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. Commented Oct 10, 2015 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). Commented Oct 10, 2015 at 11:09
  • Why so many down-votes? Explaining problem with a JSFiddle example. Did i do anything wrong? Commented Oct 10, 2015 at 11:12
  • It looks like an array, but it's an object! Good explanation here: stackoverflow.com/questions/2528680/… Commented Oct 10, 2015 at 11:12

4 Answers 4

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

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

1 Comment

Agree to @RobG. JSFiddle for reference
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);

1 Comment

balances is an array, and it is also an object. This still doesn’t answer the question.
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);

Comments

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

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.