Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Coverting JSON to JavaScript Object with just values

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)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading