title: Generate all combinations of a multidimensional object
body: <h2>Problem</h2>
  
  <p>I would like to generate all combinations for a template-like object which could take several children in which some define the desired output as an array. Those children can have as well children (there is virtually no limit).</p>
  
  <h2>Example Input</h2>
  
  <pre><code>const input = {
    a: [true, false],
    b: ['first', 'second'],
    c: {
      d: [true, false]
    }
  };
  </code></pre>
  
  <h2>Example Output</h2>
  
  <pre><code>const output = [
    {
      a: true,
      b: 'first',
      c: {
        d: true
      }
    },
    {
      a: true,
      b: 'first',
      c: {
        d: false
      }
    },
    {
      a: true,
      b: 'second',
      c: {
        d: true
      }
    },
    {
      a: true,
      b: 'second',
      c: {
        d: false
      }
    },
    //...
  ]
  </code></pre>
  
  <h2>Question</h2>
  
  <p>What could be the JavaScript function to transform the input into an output?</p>
  
  <h2>What has been tried?</h2>
  
  <p>I tried taking the original function from this question and make it more ES compliant but this is not working when having multiple children inside the object.</p>
  
  <pre><code>function combinations(input, keyIndex = 0, current = {}, result = []) {
    const keys = Object.keys(input)
    const key = keys[keyIndex]
    const values = input[key]
  
    for (const index in values) {
      current[key] = values[index]
  
      if (keyIndex + 1 &lt; keys.length) {
        combinations(input, keyIndex + 1, current, result)
      } else {
        result.push(JSON.parse(JSON.stringify(current)))
      }
    }
  
    return result;
  }
  </code></pre>