Length 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 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

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);

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

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;

Why is `Object.length === 1` in JavaScript as part of browsers?

The length of a function is the number of declared arguments, excluding any rest parameter.

From the MDN:

length is a property of a function object, and indicates how many
arguments the function expects, i.e. the number of formal parameters.
This number does not include the rest parameter.

There's exactly one for the Object constructor, the optional wrapped value.

Moreover, in the specific case of the Object function, the ES spec explicitly mandates it should have a length of 1:

Besides the internal properties and the length property (whose value is 1)

(it seems to do so for all functions taking a rest parameter, like Array or fromCharCode)

Note that the (implementation dependent) string representation of native functions in browsers doesn't necessarily show the formal arguments.

How can I calculate length on the object observer in the javascript?

In JS, objects don't have a length property, you can use the Object.keys(--your desired object--) and get its length like this Object.keys(myObject).length. I hope it helps.

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 = 0

for (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);


Related Topics



Leave a reply



Submit