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

160
Vistas
Javascript - How do I map an array of objects to a datatable with column names?

I have some data like this, an array of objects:

source = [{
  day: 1,
  deliveries: 16,
  hours: 9
}, {
  day: 2,
  deliveries: 19,
  hours: 11
}]

Which I would like to have in this format:

source = (['day', 'deliveries', 'hours'],
          ['1', '16', '9'],
          ['2', '19', '11'])

Sort of like a table. I read up a little on mapping arrays and tried this:

const datatable = source.map(d => Array.from(Object.keys(d)))
console.log(datatable)
// [["day", "deliveries", "hours"], ["day", "deliveries", "hours"]]

And this:

const datatable = source.map(d => Array.from(Object.values(d)))
console.log(datatable)
// [[1, 16, 9], [2, 19, 11]]

Each gives me half of what I want. I tried this:

let datatable = source.map(d => Array.from(Object.keys(d)))
let datatable2 = source.map(d => Array.from(Object.values(d)))

datatable = datatable[1]
let combined = datatable.concat(datatable2);
console.log(combined)
///["day", "deliveries", "hours", [1, 16, 9], [2, 19, 11]]

But even here the column names are not being combined correctly, and this way seems a little messy. How do I have the keys be on top (like column names would be) and the values following them?

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

0

Assuming you want source to be an array of arrays (common for table-like structures), get the keys once, then add each row array mapping the object's properties to the key for that index:

const keys = Object.keys(source[0]);
const result = [keys, ...source.map(obj => keys.map(key => obj[key]))];

Live Example:

const source = [{
  day: 1,
  deliveries: 16,
  hours: 9
}, {
  day: 2,
  deliveries: 19,
  hours: 11
}];

const keys = Object.keys(source[0]);
const result = [keys, ...source.map(obj => keys.map(key => obj[key]))];
console.log(result);
.as-console-wrapper {
    max-height: 100% !important;
}

Note that this assumes a couple of things:

  1. The source array always has at least one object in it.
  2. The objects in the source array all have the same set of properties.
  3. You want the keys to be in the order in which they appear in the first object in the array. (JavaScript object properties do have a defined order now, but using that order is almost never a good idea, so you might want to do some kind of sort operation on keys.)
about 4 years ago · Juan Pablo Isaza Denunciar

0

You are almost there. Just use [0] to get the first item to get only keys. And then combine the array using ....

let source = [{
  day: 1,
  deliveries: 16,
  hours: 9
}, {
  day: 2,
  deliveries: 19,
  hours: 11
}]

let ansArray = Object.keys(source[0]);
let datatable = source.map(d => Array.from(Object.values(d)))

let combined = [ansArray,...datatable];
console.log(combined);

about 4 years ago · Juan Pablo Isaza Denunciar

0

Array.from(Object.keys(d)) and Array.from(Object.values(d)) will return the wrong data if the order of property is wrong.

const source = [{
  day: 1,
  deliveries: 16,
  hours: 9
}, {
  day: 2,
  deliveries: 19,
  hours: 11
}]

const datatable = source.reduce((acc, item, index) => {
   if (index == 0) {
     acc.push(Object.keys(item));
   }
   const newRow = acc[0].reduce((rowAcc, prop) => {
     rowAcc.push(item[prop]);
     return rowAcc;
   }, []);
   
   acc.push(newRow);
  return acc;
}, [])

console.log(datatable);

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