Kirankumar Kirankumar - 5 months ago 7
Javascript Question

Count length of array of array using javascript

I have this array and I want to count total numbers into this, can anyone please let me know how can i achieve this using pure javascript

var total = {
"numbers":{
"1":"9897877884",
"2":"9867543234",
"3":"7898879900",
"4":"1234567890"
}
}

Answer

You could use Object.keys(total.numbers).length. The first part returns an array with the keys and the second gets the length of it.

var total = { "numbers": { "1": "9897877884", "2": "9867543234", "3": "7898879900", "4": "1234567890" } };
console.log(Object.keys(total.numbers).length);

Object.keys()

The Object.keys() method returns an array of a given object's own enumerable properties, in the same order as that provided by a for...in loop (the difference being that a for-in loop enumerates properties in the prototype chain as well).

Array#length

The length property represents an unsigned, 32-bit integer that is always numerically greater than the highest index in the array.