Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

125
Vistas
how i can make an array from an object if there are nested objects

I need a function that would make an array like this from an object:

let obj = {
           name: 'john',
  age: 25,
  some: {
    a: 5.5,
    b: 5,
    c: 'pls'
  }
}
 objectToArray(obj);
 // Outputs: [['name', 'john'], ['age', 25], ['some', [['a',5.5], ['b', 5], ['c','pls']]]]

How exactly should I check if there is a nested object? Is the Object.entries () method a good fit?

I think we need a function that will check whether the object is passed or not, if so, it will open it and make an array of it [key, property] using the Object.entries () method, if there is also an object, then it will call itself again and for it ... Ps: i need to use only native javascript with recursion

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

We can recurse like this:

  1. Check if the object passed in is an object using instanceof.

    • If it is continue.
    • If not return whatever the value is.
  2. Convert the object to it's entries using Object.entries.

  3. Iterate over each [key, value] pair using Array#map.

    • Recursively call objectToArray on the value. (Jump to 1.)

This is what that looks like:

const obj = { name: 'john', age: 25, some: { a: 5.5, b: 5, c: 'pls', }, };

function objectToArray(obj) {
  if (!(obj instanceof Object)) return obj;
  return Object.entries(obj).map(([key, value]) => [key, objectToArray(value)]);
}

console.log(objectToArray(obj))

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda