How to Get the Size of a JavaScript Object

Length of a JavaScript object

Updated answer

Here's an update as of 2016 and widespread deployment of ES5 and beyond. For IE9+ and all other modern ES5+ capable browsers, you can use Object.keys() so the above code just becomes:

var size = Object.keys(myObj).length;

This doesn't have to modify any existing prototype since Object.keys() is now built-in.

Edit: Objects can have symbolic properties that can not be returned via Object.key method. So the answer would be incomplete without mentioning them.

Symbol type was added to the language to create unique identifiers for object properties. The main benefit of the Symbol type is the prevention of overwrites.

Object.keys or Object.getOwnPropertyNames does not work for symbolic properties. To return them you need to use Object.getOwnPropertySymbols.

var person = {
[Symbol('name')]: 'John Doe',
[Symbol('age')]: 33,
"occupation": "Programmer"
};

const propOwn = Object.getOwnPropertyNames(person);
console.log(propOwn.length); // 1

let propSymb = Object.getOwnPropertySymbols(person);
console.log(propSymb.length); // 2

How to get object length

For browsers supporting Object.keys() you can simply do:

Object.keys(a).length;

Otherwise (notably in IE < 9), you can loop through the object yourself with a for (x in y) loop:

var count = 0;
var i;

for (i in a) {
if (a.hasOwnProperty(i)) {
count++;
}
}

The hasOwnProperty is there to make sure that you're only counting properties from the object literal, and not properties it "inherits" from its prototype.

How to efficiently count the number of keys/properties of an object in JavaScript

To do this in any ES5-compatible environment, such as Node.js, Chrome, Internet Explorer 9+, Firefox 4+, or Safari 5+:

Object.keys(obj).length
  • Browser compatibility
  • Object.keys documentation (includes a method you can add to non-ES5 browsers)

How do I find the length of an object?

You can find size of object (i.e total number of attributes in object)like this:

namelist = { "name":"xyz", "version":"1.0.0" }
var size = Object.keys(namelist).length;
console.log(size);

Output: 2

For getting size of value of name attribute ( e.g size of "xyz" in your case)

console.log(namelist.name.length)

Output: 3

For getting size of value of version attribute( e.g size of "1.0.0" in your case)

console.log(namelist.version.length)

Output: 5

I want to get size of Javascript object

This is what you are looking for:

Object.keys(productDetail).length

You are after the count of the property keys of the object.

const productDetail = new Object();productDetail["Code"] = "A104";productDetail["ProductName"] = "Keyboard";productDetail["UnitPrice"] = 10;productDetail["Dealer"] = "AMD";
console.log(Object.keys(productDetail).length)

Javascript Get length of list of items in object?

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

Length of a JavaScript object

Updated answer

Here's an update as of 2016 and widespread deployment of ES5 and beyond. For IE9+ and all other modern ES5+ capable browsers, you can use Object.keys() so the above code just becomes:

var size = Object.keys(myObj).length;

This doesn't have to modify any existing prototype since Object.keys() is now built-in.

Edit: Objects can have symbolic properties that can not be returned via Object.key method. So the answer would be incomplete without mentioning them.

Symbol type was added to the language to create unique identifiers for object properties. The main benefit of the Symbol type is the prevention of overwrites.

Object.keys or Object.getOwnPropertyNames does not work for symbolic properties. To return them you need to use Object.getOwnPropertySymbols.

var person = {
[Symbol('name')]: 'John Doe',
[Symbol('age')]: 33,
"occupation": "Programmer"
};

const propOwn = Object.getOwnPropertyNames(person);
console.log(propOwn.length); // 1

let propSymb = Object.getOwnPropertySymbols(person);
console.log(propSymb.length); // 2


Related Topics



Leave a reply



Submit