how do i recursively map and restructure an object?

You need to check to see if you have an object before you do the next recursive call. You also are resetting id so you are going to have the ids repeated (maybe you want that?) and you are using a global for the array so it can not be used more than once.

You are going to want something like:

function getKeys(obj) {

  const array = [];
  let id = 0;

  function loop(obj) {
    Object.entries(obj).forEach(entry => {
        id: ++id,
        label: entry[0],
      if(entry[1] != null && entry[1] === "Object") {


  return array;

const obj1 = { a: 1, b: 'bar' };

const obj2 = { a: 1, b: { c: 'bar' } };

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top