How to find JSON Object length in JavaScript



in array we can do something like,

let arr1 = ['apple', 'banana', 'orange', 'pineapple']

for Array,

                console.log(arr1.lenth);

But in object, that doesn't work.

let obj1 = {

                       'fruits': {

                                        0: 'apple',

                                        1: 'banana',

                                        2: 'pineapple'

                                    },

                        'vegetable': {

                                        0: 'carrot',

                                        1: 'broccoli',

                                    2: 'corn'

                                 }

                }


If you want to find the length of obj1 then you should try this,

console.log(Object.keys(obj1).length);


console.log(Object.keys(obj1[fruits]).length);

console.log(Object.keys(obj1[vegetable]).length);

2 Comments

Previous Post Next Post