Array.length is 0 even when it contains values - Javascript -


i have parent array containing child-arrays. length property on parent array displays 0 results, when there child arrays present.

here's code:

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

what's reason?

the length property works array properties numeric values. arrays objects special length property , handy methods inherited array.prototype.

when add properties kanav , yash, added 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 numeric properties  // since numbers, square bracket notation must used  arr[9] = 9;    document.write('<br>' + arr.length) // 10


Comments

Popular posts from this blog

html - Difficulties with background-image property -

visual studio code - What does the isShellCommand property actually do and how should you use it? -

ios - Segue not passing data between ViewControllers -