2

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"
    }
}
2
  • 1
    This is not an array of array. This is an object with a property that is also an object. Commented Jun 15, 2016 at 11:36
  • Ohk, so i was searching in stackoverflow about this was totally wrong Commented Jun 15, 2016 at 11:37

1 Answer 1

6

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.

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

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.