In a project using angular2/typescript, I am working with an array of objects that contain key/value pairs from a database. These values are then displayed in a table on the UI using ag-grid-ng2
.
The table headers are dynamic and set in the database.
One task I have is to convert an array of key/values into a single object format like this:
const things = [
{
field: 'some_table_header',
humanReadable: 'Some table header'
}
];
This needs to be transformed into:
const anObjectFullOfThings = {
some_table_header: 'Some table header'
};
I believe there may be a more elegant and concise way to achieve this, rather than using the current method:
let anObjectFullOfThings = {};
things.forEach((thing) => {
anObjectFullOfThings[thing.field] = thing.humanReadable;
});
I wonder if there is an alternative approach to mapping an array to object keys?