The following code is working as expected but I'm wondering if there's a way to use Object.assign
, or some other approach, to build the derivedData
object so that it doesn't need to be initialized as an empty object and then populated in a subsequent step.
const data = [ {"period": "2021-05-01", "quantity": "28"}, {"period": "2021-06-01", "quantity": "42"}, {"period": "2021-07-01", "quantity": "66"} ]; const derivedData = {}; data.forEach(o => { derivedData[o.period] = o.quantity }); console.log(data); console.log(derivedData);
Expected output of derivedData
:
{ "2021-05-01": "28", "2021-06-01": "42", "2021-07-01": "66" }