-2

How do I get the length of an object stored within a JSON?

user5508297
  • 805
  • 2
  • 9
  • 24
  • 1
    Possible duplicate of [Length of a JavaScript object (that is, associative array)](http://stackoverflow.com/questions/5223/length-of-a-javascript-object-that-is-associative-array) – André Dion Aug 12 '16 at 14:07

1 Answers1

1

Object.keys can be used to return an array of all the keys of the object and then you get the length of that array - that's the number that you are looking for.

console.log( Object.keys({"0":89,"1":54,"2":34,"3":67,"4":131}).length );
metal03326
  • 1,213
  • 2
  • 13
  • 15
  • While this code snippet may solve the question, [including an explanation](//meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Please also try not to crowd your code with explanatory comments, as this reduces the readability of both the code and the explanations! – Blue Aug 12 '16 at 14:35