function countFields(obj) { let count = 0; for (const key in obj) { if (obj.hasOwnProperty(key)) { if (typeof obj[key]!== 'object') { count++; } else { for (const subKey in obj[key]) { if (obj[key].hasOwnProperty(subKey)) { count++; } } } } } return count; } let obj={a:'1', c:{b:'2', c:{b:'2'}, c:{b:'2'}} } console.log(countFields(obj))