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

152
Vistas
Two level object to given format

I have two level nested object. I need to convert it to array.

var data = {
"6-7":
{
  "0": "06:00",
  "1": "06:10",
  "2": "06:20",
  "3": "06:30",
  "4": "06:40",
  "5": "06:50",
  "6": null,
  "7": null,
  "8": null,
  "9": null,
  "10": null,
  "11": null,
  "12": null,
  "13": null,
  "14": null
},
"7-8": 
{
  "0": "07:00",
  "1": "07:04",
  "2": "07:08",
  "3": "07:12",
  "4": "07:16",
  "5": "07:20",
  "6": "07:24",
  "7": "07:28",
  "8": "07:32",
  "9": "07:36",
  "10": "07:40",
  "11": "07:44",
  "12": "07:48",
  "13": "07:52",
  "14": "07:56"
}
}

I tried Object.values(obj).map(elem => elem)

it converted to array but still i can't convert it right. I need to convert it like [["06:00","06:10",...],[07:00,07:04,...]]

Thanks for any kind of help.

Edit : data is always same shape not going deeper

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

0

You want just the values of the values, so its as simple as:

const result = Object.values(data).map( v => Object.values(v) );

If you need to filter out the nulls then its a tiny bit extra

const result = Object.values(data).map( v => Object.values(v).filter(x => x != null) );

Live example of the latter

var data={"6-7":{0:"06:00",1:"06:10",2:"06:20",3:"06:30",4:"06:40",5:"06:50",6:null,7:null,8:null,9:null,10:null,11:null,12:null,13:null,14:null},"7-8":{0:"07:00",1:"07:04",2:"07:08",3:"07:12",4:"07:16",5:"07:20",6:"07:24",7:"07:28",8:"07:32",9:"07:36",10:"07:40",11:"07:44",12:"07:48",13:"07:52",14:"07:56"}};

const result = Object.values(data).map( v => Object.values(v).filter(x => x != null) );
console.log(result)

about 4 years ago · Juan Pablo Isaza Denunciar

0

var data = {
"6-7":
{
  "0": "06:00",
  "1": "06:10",
  "2": "06:20",
  "3": "06:30",
  "4": "06:40",
  "5": "06:50",
  "6": null,
  "7": null,
  "8": null,
  "9": null,
  "10": null,
  "11": null,
  "12": null,
  "13": null,
  "14": null
},
"7-8": 
{
  "0": "07:00",
  "1": "07:04",
  "2": "07:08",
  "3": "07:12",
  "4": "07:16",
  "5": "07:20",
  "6": "07:24",
  "7": "07:28",
  "8": "07:32",
  "9": "07:36",
  "10": "07:40",
  "11": "07:44",
  "12": "07:48",
  "13": "07:52",
  "14": "07:56"
}
}

const array = Object.values(data).map(Object.values)

console.log(array);

about 4 years ago · Juan Pablo Isaza Denunciar

0

Below is one possible way to achieve the target.

Code Snippet

// recursive method to convert object to array
const myConv = obj => (
  Object.entries(obj).flatMap(          // iterate over key-value pairs
    ([k, v]) => (
        v
        ? typeof v === 'object'     // if nested value is object
          ? [myConv(v)]             // nested-array & make the recursive call
          : v                       // else, simply return the value as-is
        : null                      // if value is null, return null
      )
  ).filter(Boolean)                 // filter to remove falsy values
);

const data = {
  "6-7": {
    "0": "06:00",
    "1": "06:10",
    "2": "06:20",
    "3": "06:30",
    "4": "06:40",
    "5": "06:50",
    "6": null,
    "7": null,
    "8": null,
    "9": null,
    "10": null,
    "11": null,
    "12": null,
    "13": null,
    "14": null
  },
  "7-8": {
    "0": "07:00",
    "1": "07:04",
    "2": "07:08",
    "3": "07:12",
    "4": "07:16",
    "5": "07:20",
    "6": "07:24",
    "7": "07:28",
    "8": "07:32",
    "9": "07:36",
    "10": "07:40",
    "11": "07:44",
    "12": "07:48",
    "13": "07:52",
    "14": "07:56"
  }
};

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

Explanation

Inline comments added in the snippet above.

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