Apologies for any confusion caused by the title. As a newcomer to JavaScript, I may not be able to articulate exactly what I am trying to achieve. Hence, I will showcase my code and explain the desired outcome instead.
Here is an array of objects I am working with:
const mi_array = [{
text: "1st title",
col2015: 81.8,
col2016: 86.4,
col2017: 67.3,
col2018: 70.8,
col2019: 67.6
},{
text: "2nd title",
col2015: 90.8,
col2016: 67.4,
col2017: 39.3,
col2018: 50.8,
col2019: 95.6
}];
My desired output should resemble the following:
const new_array = [{
name: "1st title",
data: [81.8,86.4,67.3,70.8,67.6]
},{
name: "2nd title",
data: [90.8,67.4,39.3,50.8,95.6]
}];
I have been exploring how to achieve this transformation, but the closest solution I found is not quite what I need:
const new_array = [];
mi_array.forEach(value => {
for (let key in value) {
new_array.push(value[key]);
}
});
console.log(new_array);
However, the current output is not as expected:
["1st title", 81.8, 86.4, 67.3, 70.8, 67.6, "2nd title", 90.8, 67.4, 39.3, 50.8, 95.6];