Mastering in Javascript:
As a JavaScript developer, we have to handle multiple conditions with objects to match our requirements.
So sometimes we need to find the length of the object.
So sometimes we need to find the length of the object.
Get the size of JSON object:
To get the size of an array we use array.length but for the object, it won’t work, let’s see it by example.
var arrObj = {'first_name':'Vikas', 'last_name:':'Kad','age': 26}
The output will be like this:If you see output arrObj.length shows undefinedTo find the size of object we will find it using Object property.var count = Object.keys(arrObj).length; console.log('Object length is: ',count); >>> Output Object length is: 3
So inshort by using Object.keys() Javascript Object function you can find the size of object easily.
The Object.keys()
method returns an array of a given object’s own propertynames
in the same order as we get with a normal loop.That’s it, comment if you have any doubts.