0

I have an object like this.

{Brunch: 2, Kimchi: 1}

I need to refactor it into an an array/object

[{
"label" : "Brunch",
"value" : 2
},
{
"label" : "Kimchi",
"value" : 1
}]
The Old County
  • 89
  • 13
  • 59
  • 129

2 Answers2

4

You can use Object.keys() and map() to get desired result.

var obj = {
  Brunch: 2,
  Kimchi: 1
}

var result = Object.keys(obj).map(function(k) {
  return {
    "label": k,
    "value": obj[k]
  }
})

console.log(result)
Nenad Vracar
  • 118,580
  • 15
  • 151
  • 176
0

The simplest way:

var result = Object.keys(input).map(key => ({
    label: key,
    value: input[key],
}));
Azamantes
  • 1,435
  • 10
  • 15