25

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?

Community
  • 1
  • 1
John Cooper
  • 6,277
  • 26
  • 72
  • 97
  • 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. – BoltClock Jul 17 '11 at 11:49
  • http://stackoverflow.com/questions/5223/length-of-javascript-associative-array – felix Jul 17 '11 at 11:50
  • 1
    Further to @BoltClock's comment, [`alert(values.length);` gives: `1`](http://jsfiddle.net/davidThomas/UvthC/). 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... =) – David says reinstate Monica Jul 17 '11 at 11:51

4 Answers4

69
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);
Petar Ivanov
  • 84,604
  • 7
  • 74
  • 90
5

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;
}
beardhatcode
  • 3,895
  • 1
  • 13
  • 27
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.

phihag
  • 245,801
  • 63
  • 407
  • 443
-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
pimvdb
  • 141,012
  • 68
  • 291
  • 345