What is the best way to convert:
[2019,2020,2021]
to
{
0: {year:2019},
1: {year:2020},
2: {year:2021}
}
What is the best way to convert:
[2019,2020,2021]
to
{
0: {year:2019},
1: {year:2020},
2: {year:2021}
}
Please try this:
a = [2019,2020,2021];
a.reduce((acc, val, idx)=> {acc[idx] = {year: val}; return acc;}, {});
Combination of Object.assign()
and array.map()
comes to mind:
const array = [2019,2020,2021];
const object = Object.assign({}, array.map(a => ({year: a})));
console.log("object:", object);
const object2 = {}
array.forEach((a, i) => object2[i] = {year: a});
console.log("object2:", object2);