I have this object that looks like that
{taxi: 1, food: 1, cinema: 2, drinks: 2}
My goal is to define a name for those properties like so :
const expenseCategories = [
{ name: 'taxi', value: 1 },
{ name: 'food', value: 1 },
{ name: 'cinema', value: 2 },
{ name: 'drinks', value: 2},
];
Here is my attempt, I am nowhere near what I want to accomplish. I know it's simple but I got confused..
var test = [];
for (let x in expenseCategories) {
// expenseCatAmountsObj.push(expenseCategories[x]);
test.push( Object.defineProperty({}, {name : x}, { value: expenseCategories[x] }) )
}
It just returns an array inside object
[{…}, {…}, {…}, {…}]
test.push({ name: x, value: expenseCategories[x] })orvar test = Object.keys(expenseCategories).map(key => { name: key, value: expenseCategories[key] });Object.assigninstead ofdefineProperty. Otherwise, just push the item in a single shot as Chris suggested.