0

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

[{…}, {…}, {…}, {…}]

  • 3
    `test.push({ name: x, value: expenseCategories[x] })` or `var test = Object.keys(expenseCategories).map(key => { name: key, value: expenseCategories[key] });` –  May 20 '19 at 11:40
  • If you want to keep the code as it is, just use `Object.assign` instead of `defineProperty`. Otherwise, just push the item in a single shot as Chris suggested. – briosheje May 20 '19 at 11:40
  • Possible duplicate of [How to convert an object to array of objects](https://stackoverflow.com/questions/49628432/how-to-convert-an-object-to-array-of-objects) and [How to convert an object to array of objects](https://stackoverflow.com/questions/49628432) – adiga May 20 '19 at 12:09

5 Answers5

1

You can use Object.entries and Array.prototype.map

let obj = {taxi: 1, food: 1, cinema: 2, drinks: 2}

let out = Object.entries(obj).map(([name, value]) => ({name, value}));
console.log(out)
AZ_
  • 3,094
  • 1
  • 9
  • 19
0

We can loop over the keys of the object using map, which will return an array of results:

let categories = {taxi: 1, food: 1, cinema: 2, drinks: 2};

let test = Object.keys(categories).map(k => {
    return { name: k, value: categories[k] }
});

console.log(test);
Tim VN
  • 1,183
  • 1
  • 7
  • 19
0
const temp = {taxi: 1, food: 1, cinema: 2, drinks: 2};

let expenseCategories = Object.keys(temp).map(key => ({name: key, value: temp[key]}));
0

You have the steps almost right - only Object.defineProperty({}, {name : x}, { value: expenseCategories[x] }) is unneeded, since it's overly complex way of doing what you want. Instead just use an object literal:

var expenseCategories = { taxi: 1, food: 1, cinema: 2, drinks: 2 }

var test = [];

for (let x in expenseCategories) {
  test.push(
    {
      name: x,
      value: expenseCategories[x]
    }
  )
}

console.log(test)

An alternative is to iterate over Object.entries and produce an array using Array#map, destructuring, and short object initialisation syntax:

var expenseCategories = { taxi: 1, food: 1, cinema: 2, drinks: 2 }

var test = Object.entries(expenseCategories)
  .map(([name, value]) => ({name, value}))

console.log(test)
VLAZ
  • 26,331
  • 9
  • 49
  • 67
0

You can use Object.keys

let obj = {taxi: 1, food: 1, cinema: 2, drinks: 2}
let arr = []

let keys = Object.keys(obj)

for (let i = 0; i < keys.length; i++) {
    let key = keys[i]
    arr[i] = {name: key, value: obj[key]}
}

console.log(arr)
Oliver Nybo
  • 560
  • 1
  • 6
  • 24