Apply arbitrary action during recursive generator function I've got this recursive generator function. It will traverse an object looking for "leafs", any keys that don't point to sub-objects.

Then it applies one of two actions on the leaf and yields it.
Any suggestions for improvement would be appreciated. :)



const obj = {
  a: 'a',
  b: 'b',
  c: {
    d: 'd',
    e: 'e',
  }
};

const clone = toClone =&gt; JSON.parse(JSON.stringify(toClone))

function* leafIterator(object, action) {
  for (const key of Object.keys(object)) {
    if (typeof object[key] === 'object') {
      for (const inner of leafIterator(object[key], action)) {
        let oclone = clone(object);
        oclone[key] = inner;
        yield oclone;
      }
    }
    else {
      let oclone = clone(object);
      action(oclone, key);
      yield oclone;
    }
  }
}

for (let o of leafIterator(obj, (s, key) =&gt; delete s[key])) {
  console.log('result', o);
}
Good things:


all lines terminated with a semi-comon
strict equality used when checking for objects


I would suggest using const by default since none of the variables/functions get reassigned. If you determine reassignment is necessary then use let. This will help avoid accidental re-assignment.

And is the variable naming oclone Hungarian notation? If so, why not use it for all variables instead of cloned variables?