Navigate back to the homepage

How to get count of json object in javascript?

Infinitbility
JavaScript
May 18th, 2022 · 1 min read

Hi Friends 👋,

Welcome To Infinitbility! ❤️

Today, we are going to learn How to get a count of a JSON object in javascript, here we will use the keys() method and array length to get a count of a JSON object.

Basically, The keys() method creates an array of object keys, and when we got an array we will use simple array length syntax to get a count of objects.

Well, let’s start today’s tutorial How to get a count of a JSON objects in javascript?

Here, we will do

  1. create a sample object variable
  2. use Object.keys() method to get an array
  3. use .length to get the number of keys
1// create sampel oject variable
2let sampleObject = {
3 id: 1,
4 name: "infinitbility",
5 domain: "infinitbility.com",
7};
8
9// convert keys to array
10let sampleObjectKeys = Object.keys(sampleObject);
11console.log("sampleObjectKeys", sampleObjectKeys)
12
13// print number of keys
14console.log("number of keys", sampleObjectKeys.length);

we can also write in one line like the below example.

1console.log(Object.keys(sampleObject).length);

let’s run it, it should show the 4 size of the object.

Output

javascript, get count of object
javascript, get count of object

All the best 👍.

Follow me on Twitter

Join our email list and get notified about new content

No worries, I respect your privacy and I will never abuse your email.

Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Guide book

Guidebook: The Javascript Monorepo Photoshop Guide

Tutorials

Categories

More articles from Infinitbility

How to remove square brackets from json object in javascript?

Example of remove square brackets from json object in javascript with step by step guide.

May 18th, 2022 · 1 min read

How to get all values from json object in javascript?

Example of get all values from json object in javascript with step by step guide.

May 17th, 2022 · 1 min read
© 2020–2022 Infinitbility
About
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]