Reputation: 99
I have this object
{TDD_rating: 80, Fluency_rating: 70, Debug_rating: 64, Model_rating: 53, Refactor_rating: 68,}
which I want in the format of this array
[
{ subject: 'TDD', score: 80 },
{ subject: 'Fluency', score: 70},
{ subject: 'Debug', score: 65},
{ subject: 'Model', score: 53},
{ subject: 'Refactor', score: 68},
];
I have tried to use Object.entries and map but can't seem to get there
Upvotes: 0
Views: 84
Reputation: 3302
You could use combination of Object.entries()
and Array.prototype.map()
method to get your result. Get the key value pairs using Object.entries()
method and then map it to make your required object array.
const data = {
TDD_rating: 80,
Fluency_rating: 70,
Debug_rating: 64,
Model_rating: 53,
Refactor_rating: 68,
};
const ret = Object.entries(data).map(([x, y]) => ({
subject: x.replace('_rating', ''),
score: y,
}));
console.log(ret);
Upvotes: 1
Reputation: 5054
You can use Object.keys and map method to achieve that,
const obj = {
TDD_rating: 80,
Fluency_rating: 70,
Debug_rating: 65,
Model_rating: 53,
Refactor_rating: 68,
};
const result = Object.keys(obj).map((key) => ({ subject: key, score: obj[key] }));
console.log(result);
Upvotes: 1