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
  • 3
    test.push({ name: x, value: expenseCategories[x] }) or var test = Object.keys(expenseCategories).map(key => { name: key, value: expenseCategories[key] }); Commented May 20, 2019 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. Commented May 20, 2019 at 11:40
  • Possible duplicate of How to convert an object to array of objects and How to convert an object to array of objects Commented May 20, 2019 at 12:09

5 Answers 5

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)

Sign up to request clarification or add additional context in comments.

Comments

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);

Comments

0
const temp = {taxi: 1, food: 1, cinema: 2, drinks: 2};

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

Comments

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)

Comments

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.