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

Aman

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?

T.J. Crowder

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I convert array of Objects into one Object in JavaScript?

How do I remove objects from a JavaScript associative array?

How do I convert an array of objects into an object (dynamically) in JavaScript

How do I convert array of objects to object in javascript?

How do I merge an array of objects in Javascript?

JavaScript: How can I change property names of objects in an array?

How do I map an array of numbers to a property in an array of objects?

How can i sort javascript.map array of names by ABC

How do I return an array of objects with certain conditions in Javascript?

How do I leave a fixed column in DataTable?

How do I convert array of Objects to one Object in JavaScript?

How do I filter an array object inside of a array of objects Javascript?

How can I map the names of an array of objects to change the field names?

Javascript getting Datatable column names

How do I convert this php array into a JavaScript array of objects?

How do I create a javascript array of lists of objects?

How do I reuse objects in an array for a particle system in JavaScript/jQuery?

How do I call a method on an array of objects in javascript?

How do I map an array of objects

How do I put all column names in a php array?

How do I map through this set of objects within array of objects in objects

How do I check if a particular object exist or not in a JavaScript array of objects?

How do I declare the objects having array of objects in JavaScript?

How do I modify an object value in a cloned array of objects in Javascript

How can i convert a javascript dynamic array to an array of multiple objects with custom key names

How do I map through an array of nested objects?

How do I map array of objects to an existing array on MongoDB

How do I list Javascript array of objects in HTML

Using JavaScript, how to join two arrays of objects based on array of column names?