JSFiddle - React, Tailwind, and code Playground
by ishanbakshi
HTML
<h1>
Here we see how to count the number of fields in a JSON object
</h1>
<p>
</p>
JavaScript
const sampleObj = {
prop1: 'val1',
prop2: 'val2',
prop3: 21,
innerObj: {
innerProp1: 'val1',
innerProp2: 'val2'
}
};
let count = 0;
for (var i in sampleObj) {
count = count + 1;
}
console.log(count);// 2
document.getElementsByTagName("p")[0].innerHTML="Number of properties in the object = "+count;