cachaito
10/17/2019 - 6:43 PM

Recursively collect all ketys from deep neste objects

function getKeys(obj) {
  return Object.keys(obj).reduce((r, k) => {
    r.push(k);

    if (Array.isArray(obj[k])) {
      obj[k].forEach(itemInArr => {
        getKeys(itemInArr);
      });
    } else if (Object(obj[k]) === obj[k]) {
      r.push(...getKeys(obj[k]));
    }

    return r;
  }, []);
}