29

Possible Duplicate:
How to efficiently count the number of keys/properties of an object in JavaScript?

var values = [{
    'SPO2': 222.00000,
    'VitalGroupID': 1152,
    'Temperature': 36.6666666666667,
    'DateTimeTaken': '/Date(1301494335000-0400)/',
    'UserID': 1,
    'Height': 182.88,
    'UserName': null,
    'BloodPressureDiastolic': 80,
    'Weight': 100909.090909091,
    'TemperatureMethod': 'Oral',
    'Resprate': null,
    'HeartRate': 111,
    'BloodPressurePosition': 'Standing',
    'VitalSite': 'Popliteal',
    'VitalID': 1135,
    'Laterality': 'Right',
    'HeartRateRegularity': 'Regular',
    'HeadCircumference': '',
    'BloodPressureSystolic': 120,
    'CuffSize': 'XL'
}];

for (i=0; i < values.length; i++) {
        alert(values.length) // gives me 2. 

How can find how many keys my object has?

3
  • 1
    Two things: 1) Why are you looping through values.length yet also alerting values.length? 2) I swear values.length is not going to give you 2. Commented Jul 17, 2011 at 11:49
  • stackoverflow.com/questions/5223/… Commented Jul 17, 2011 at 11:50
  • 1
    Further to @BoltClock's comment, alert(values.length); gives: 1. And, incidentally, from my own training, the saturated partial arterial pressure of oxygen (SPO2), should be a percentage value of up to 100 (percent, obviously). Where's the 222.00000 coming from? I'm not intending to be confrontational, but I am curious... =) Commented Jul 17, 2011 at 11:51

4 Answers 4

74
var value = {
    'SPO2': 222.00000,
    'VitalGroupID': 1152,
    'Temperature': 36.6666666666667,
    'DateTimeTaken': '/Date(1301494335000-0400)/',
    'UserID': 1,
    'Height': 182.88,
    'UserName': null,
    'BloodPressureDiastolic': 80,
    'Weight': 100909.090909091,
    'TemperatureMethod': 'Oral',
    'Resprate': null,
    'HeartRate': 111,
    'BloodPressurePosition': 'Standing',
    'VitalSite': 'Popliteal',
    'VitalID': 1135,
    'Laterality': 'Right',
    'HeartRateRegularity': 'Regular',
    'HeadCircumference': '',
    'BloodPressureSystolic': 120,
    'CuffSize': 'XL'
};

alert(Object.keys(value).length);
Sign up to request clarification or add additional context in comments.

1 Comment

How can i find the total number of keys if the Object is inside an array
7

try

Object.keys(values).length

see: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/keys

for compatiblity

if(!Object.keys) Object.keys = function(o){
 if (o !== Object(o))
      throw new TypeError('Object.keys called on non-object');
 var ret=[],p;
 for(p in o) if(Object.prototype.hasOwnProperty.call(o,p)) ret.push(p);
 return ret;
}

or use:

function numKeys(o){
var i=0;
for(p in o) if(Object.prototype.hasOwnProperty.call(o,p)){ i++};
return i;
}

1 Comment

which you should attribute to its source...
5
function numKeys(o) {
   var res = 0;
   for (var k in o) {
       if (o.hasOwnProperty(k)) res++;
   }
   return res;
}

or, in newer browsers:

function numKeys(o) {
   return Object.keys(o).length;
}

In your example, values is an array with one element, so you'd call numKeys(values[0]) to find out.

1 Comment

#2 should be Object.keys(o).length
-2

You can iterate and just count:

var i = 0;
for(var key in values[0]) if(values[0].hasOwnProperty(key)) i++;
// now i is amount

3 Comments

This will include properties defined by the object prototype
@phihag: You're correct but you should not extend Object.prototype anyway.
@pimvdb indeed you shouldn't, but you should program defensively in case one of the libraries you're using has.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.