arraysjsonstructuremap

how to remap a json array


im trying to remap the following JSON object

{At Risk: 803, Champions: 847, Need Attention: 275, Hibernating: 1463, Loyal Customers: 1137, …}

I would like the result to be like this

[{
  Segment: "At Risk",
  value: 803
  }, {
  Segment: "Champions",
  value: 847
  }, {
  Segment: "Need Attention",
  value: 275
  }, {
  Segment: "Hibernating",
  value: 1463
  }, {
  Segment: "Loyal Customers",
  value: 113 
}]

Any help is appreciated!


Solution

  • var a = {'At Risk': 803, 'Champions': 847, 'Need Attention': 275, 'Hibernating': 1463, 'Loyal Customers': 1137};
    
    var arr = Object.keys(a).map((k) => ({Segment: k, value: a[k]}));
    console.log(arr);