title: count the total number of keys from nested objects and arrays
body: <p>I have a nested object with different types of properties including string, object and array. I used the recursive approach to count the total number of keys (from key1 to key9) in the object, but failed to achieve the correct solution. Example of my code below.</p>
  <pre><code>const data = {
    key1: '1',
    key2: '2',
    key3: { key4: '4' },
    key5: [
      {
        key6: '6',
        key7: '7',
      },
      {
        key8: '8',
        key9: '9',
      }
    ]
  }
  
  const countKeys = (data) =&gt; {
    let count = 0;
  
    const helper = (data) =&gt; {
      for (let value of Object.values(data)) {
        if (typeof value === 'object') {
          helper(value);
        }
        // this line is problematic as it counts each object in the array as key. 
        if (!Array.isArray(value)) count++;
      }
    }
    helper(data)
    return count;
  }
  console.log(countKeys(data)) // 10, correct answer is 9
  </code></pre>
  <p>I try to wrap my head around for many hours but couldn't get the solution. Can anyone explain me what I'm missing here and supposed to add to make it work?</p>