Here is a small sample of data that's representative of a larger dataset:
const data = [
{ id: 32, minimum: 200, maximum: 400 },
{ id: 16, minimum: 370, maximum: 390 },
{ id: 85, minimum: 700, maximum: 950 }
];
There is a need to create an object from this data using the id
as the key and having the min and max as the value, like so:
The current approach for creating this object is the following -
let object = {};
data.forEach(o => {
object[o.id] = { min: o.minimum, max: o.maximum }
});
console.log(object);
While this works, it seems like there should be a more concise way of writing this code.
I have experimented with .map()
-
const obj = data.map(o => ({[o.id]: { min: o.minimum, max: o.maximum }}));
console.log(obj);
but the output is not in the correct format -
What should be changed in the .map()
version to achieve the desired output format?