Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

147
Views
How to recursively map/restructure an object?

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'
    }
  }
}}
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

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));

about 4 years ago · Juan Pablo Isaza Report

0

Perhaps you may do like

var data = {a: {
  b: {
    c: {
      foo: 'bar',
      arr: [1,2,3,4]
    }
  }
}};

function getAllKeys(obj){
  var keys = (typeof obj === "object") && (obj !== null) && Object.keys(obj);
  return !!keys ? keys.reduce((r,k) => r.concat(getAllKeys(obj[k])),keys)
                : [];
};

var res = getAllKeys(data);

console.log(JSON.stringify(res));

about 4 years ago · Juan Pablo Isaza Report

0

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 }

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!