0

I have some JSON where I need to convert just the values to an Object. How would I go about this?

My JSON is

[
{
  "WebsiteName": "Acme Inc.",
  "Time": "08:30:00",
  "TheDate": "2021-12-23",
  "Hits": "39"
},
{
  "WebsiteName": "Acme Inc.",
  "Time": "08:45:00",
  "TheDate": "2021-12-23",
  "Hits": "37"
}
]

and I am trying to format it like so (without the names)

var myObject = [["Acme Inc.", "08:30:00", "2021-12-23", "39"], ["Acme Inc.", "08:45:00", "2021-12-23", "37"]];

I need to do this with assuming that the code doesn't know the names. That way I can reuse the code on another JSON file without having to tweak the code each time.

N30C0rt3X
  • 31
  • 7

1 Answers1

4

Just .map it with Object.values:

let json = `[
{
  "WebsiteName": "Acme Inc.",
  "Time": "08:30:00",
  "TheDate": "2021-12-23",
  "Hits": "39"
},
{
  "WebsiteName": "Acme Inc.",
  "Time": "08:45:00",
  "TheDate": "2021-12-23",
  "Hits": "37"
}
]`;

let result = JSON.parse(json).map(e => Object.values(e));

console.log(result);
NullDev
  • 6,739
  • 4
  • 30
  • 54