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
Insert HTML at Caret in a Contenteditable Div
What Values Can a Constructor Return to Avoid Returning This
How to Access the First Property of a JavaScript Object
Convert String with Commas to Array
How to Get the File Name from a Full Path Using JavaScript
How to Upload a File with the Js Fetch API
JavaScript Function Aliasing Doesn't Seem to Work
The Value of "This" Within the Handler Using Addeventlistener
How to Replace While Loops with a Functional Programming Alternative Without Tail Call Optimization
Does IE9 Support Console.Log, and Is It a Real Function
Why Does the Month Argument Range from 0 to 11 in JavaScript's Date Constructor
How to Pass JSON Post Data to Web API Method as an Object
Jquery - Setting the Selected Value of a Select Control via Its Text Description
React/Jsx Dynamic Component Name
What Does = +_ Mean in JavaScript
What's the Difference Between Window.Location and Document.Location in JavaScript