Presented here is a subset of data extracted from a larger dataset in a project:
const data = [
{ scenario: '328', buckets: 2 },
{ scenario: '746', buckets: 1 },
{ scenario: '465', buckets: 2 }
];
The task at hand involves restructuring the data into an object where the scenario value serves as the key and the bucket value as the corresponding value, illustrated in this format: https://i.sstatic.net/PZLD6.png
I managed to accomplish this goal using .forEach
method like this:
const reformattedData = {};
data.forEach(o => {
reformattedData[o.scenario] = o.buckets;
});
However, I am under the impression that there might be a more succinct way to achieve the same result. My attempt with .map
was as follows:
const reformattedData = data.map(o => ({ [o.scenario] : o.buckets}));
Unfortunately, this approach does not produce the desired outcome. Instead, it yields [{328: 2}, {746: 1}, {465: 2}]
. What adjustments are required in the .map()
version to obtain the intended result?