If you want to know the number of unique properties of Object
s in an Array
, this should do it...
var uniqueProperties = [];
for (var i = 0, length = arr.length; i < length; i++) {
for (var prop in arr[i]) {
if (arr[i].hasOwnProperty(prop)
&& uniqueProperties.indexOf(prop) === -1
) {
uniqueProperties.push(prop);
}
}
}
var uniquePropertiesLength = uniqueProperties.length;
jsFiddle.
Note that an Array
's indexOf()
doesn't have the best browser support. You can always augment the Array
prototype (though for safety I'd make it part of a util
object or similar).