What is the length of a JavaScript object?
Object.size = function(obj) {
var size = 0,
key;
for (key in obj) {
if (obj.hasOwnProperty(key)) size++;
}
return size;
};
// Get the size of an object
const myObj = {}
var size = Object.size(myObj);
var size = Object.keys(myObj).length;
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
Tags: javascript javascript-objects
Source: By Gareth Simpson as answer to the question
This code snippet was collected from stackoverflow, and is licensed under CC BY-SA 4.0
Related code-snippets:
- How do I know which font has been used in Web page?
- ASP.Net Custom Client-Side Validation. ASP.Net has created thousands of Web Application Test cases.
- What do I do with triple quotes? How do I delimit a databound Javascript string parameter in ASP.NET?
- How do I make the browser see CSS and JavaScript changes?
- How do I set the background color of HTML element using CSS properties in JavaScript?
- How do I capture TAB key in text box?
- How do I call ASP.NET functions from JavaScript?
- Can I turn a string of HTML into a DOM object in Firefox?
- How do I use http authentication in Firefox?
- What is a wrapper for lists in columns?
- How do you set up a CSS switcher to run your website in a browser?
- How do I upload a request in ASP.NET and redirect it to external domains?
- How to specify javascript to run when ModalPopupExtender is shown?
- How do you get a text from a drop-down box?
- What's the difference in closure style?