Miller
Miller

Reputation: 145

Creating highchart piechart with a javascript object

I am using highchart to create a graph. I have a javsascript object like this

test1:38
test2:2
test3:160

I want to create a piechart with the values of this object something like this

series: [{

          name: 'Success',
          data: [
          {name:"test1", y:1},
         {name:"test2", y:38},
         {name:"test3", y:k},]
}]

How can I create the data array like this using my javascript object.I am new to javascript so any help would be appreciated.

Upvotes: 0

Views: 37

Answers (1)

adiga
adiga

Reputation: 35263

You could map the entries of the object to get the data array

const input = {
  test1: 38,
  test2: 2,
  test3: 160
}

const data = Object.entries(input)
                   .map(([name, y]) => ({ name, y }))

console.log(data)

const chart = {
  series: [{
    name: 'Success',
    data
  }]
}

console.log(chart)

Upvotes: 1

Related Questions