Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
62
rated 0 times [  67] [ 5]  / answers: 1 / hits: 42391  / 8 Years ago, mon, may 30, 2016, 12:00:00

I currently have a Javascript object that looks like this:



Object {0: 8, 1: 9, 2: 10}



I am trying to get the number of individual items in the object (i.e. 3) but can't figure out how to do so. Since the object isn't an array, I can't just call .length(). I tried huntsList[2].toString().split('.').length to split the items at the commas and count them in this way but it returns 1, since it converts the entire object to a single string that looks like this: [[object Object]].



Any suggestions for how I can accomplish this are appreciated.


More From » object

 Answers
41

You could get the keys using Object.keys, which returns an array of the keys:



Example



var obj = {0: 8, 1: 9, 2: 10};

var keys = Object.keys(obj);

var len = keys.length

[#61971] Friday, May 27, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
dominickmackenziet

Total Points: 583
Total Questions: 101
Total Answers: 117

Location: Saint Lucia
Member since Wed, Feb 8, 2023
1 Year ago
dominickmackenziet questions
Wed, Apr 7, 21, 00:00, 3 Years ago
Fri, Feb 12, 21, 00:00, 3 Years ago
;