JavaScript Solutions, Competitive programming in JavaScript, MCQ in JS

Wednesday, 23 January 2019

Mastering in Javascript | get size of json object

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.

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:

get size of json object

If you see output arrObj.length shows undefined  

To 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 property names in the same order as we get with a normal loop.

That's it, comment if you have any doubts.

No comments:

Post a Comment