Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript Array.length returning 0

I have javascript Array that looks like enter image description here

The problem is Object.mappings has 3 elements which are clearly printed in console with console.log(), but when I try to find length of the array it returns 0. I tried stringifying the array and it just prints "[]".

what Am I doing wrong here?

like image 597
Anonymous Zombie Avatar asked Oct 21 '25 11:10

Anonymous Zombie


1 Answers

You can use length property only if array contains elements associated with indexes. If it is associative array then length property will return 0. You can count elements by yourself using this code:

function len(arr) {
  var count = 0;
  for (var k in arr) {
    if (arr.hasOwnProperty(k)) {
      count++;
    }
  }
  return count;
}

Then you can count elements of "mappings" array using len(mappings)

like image 60
Konstantins Bogdanovs Avatar answered Oct 22 '25 23:10

Konstantins Bogdanovs



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!