SOURCE

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))
console 命令行工具 X clear

                    
>
console