How to Get Object 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

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 get the length of an object javascript

var length = Object.keys(Obj).length;

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

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

Object of array of objects. Get the length of all values with the same property

A straight-forward approach:

let obj = {      "array1": [        {"label": "something1", "ref": "option2a"},        {"label": "something2", "ref": "option2b"},        {"label": "something3", "ref": "option2a"},        {"label": "something4", "ref": "option2a"},        {"label": "something5", "ref": "option2a"}      ],      "array2": [        {"label": "something6 is the longest label", "ref": "option3a"},        {"label": "Other", "ref": "option3b"}      ]};
let longest = 0for (key in obj){ if (!obj.hasOwnProperty(key)) continue; for (let x of obj[key]){ if (x.label.length > longest) longest = x.label.length; }}console.log(longest);

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)

Object length in typescript?

You could try the following:

Object.keys(customer).length

How to get the length of an Object

Objects don't have a length, you will need to use Object.key like in example:

let array = {  "parent": [{    "child": {      "0": 1,      "1": 1,      "2": 1    }  }]};

console.log(Object.keys(array.parent[0].child).length);


Related Topics



Leave a reply



Submit