Home > Net >  How to recursively map/restructure an object?
How to recursively map/restructure an object?

Time:01-04

I would like an array of objects with all object keys from a nested object. I wrote a recursive function to do this however at the point that the function is recalled it is not going through the object as expected but rather sending back an index infinitely.

let array = [];

const findKeys = (ob) => {
  let id = 0;
  let keys = Object.keys(ob);
  for (let i = 0; i < keys.length; i  ) {

    let object = {
      id: id,
      label: keys[i],
    };

    array.push(object);
    id   ;
    findKeys(ob[keys[i]]);
  }
  return array;
};
let newArray = findKeys(data);
console.log(newArray);

example data structure:

const data = {a: {
  b: {
    c: {
      foo: 'bar'
    }
  }
}}

CodePudding user response:

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 => {
      array.push({
        id:   id,
        label: entry[0],
      });      
      if(entry[1] != null && entry[1].constructor.name === "Object") {
        loop(entry[1]);
      }
    });
  }

  loop(obj);

  return array;
}

const obj1 = { a: 1, b: 'bar' };
console.log(getKeys(obj1));

const obj2 = { a: 1, b: { c: 'bar' } };
console.log(getKeys(obj2));

CodePudding user response:

some thing like that

see also Check that value is object literal?

const data = { a: { b: { c: { foo: 'bar' } } }}

const isObject = el => (Object.prototype.toString.call(el) === '[object Object]')

const findKeys = obj => 
  {
  let arr   = []
    , id    = 0
    ;
  getKeys(obj)
  return arr

  function getKeys(o)
    {
    Object.keys(o).forEach(key =>
      { 
      arr.push({ id:id  , label:key })
      if (isObject(o[key]))
        getKeys(o[key])
      })  
    }
  }
  

console.log( findKeys(data) )
.as-console-wrapper {max-height: 100%!important;top:0 }

  • Related