0

I have a function that returns the data as JSON. Due to the uncertainty of the return however, I need a way to grab all the key names and return as an Array.

I have tried to manually parse it as a String, however it does not seem to be working as there are different ways to format it. I have also tried to just use JSON normally and access it as myJSON.a however as I already said, it has unpredictable returns. To visualize it:

// This could be what the function returns
{
a:"a",
b:"b",
c:"c"
}```
// Or this
{
d:"d",
a:"a",
c:"c"
}
// So accessing "myJSON.b" won't always work
TheDude53
  • 27
  • 1
  • 4

1 Answers1

0

basically using just Object.keys() will do the trick for you.

const resultFromFetch = {
  a: "a",
  b: "b",
  c: "c",
  'entry1': 'test'
}

const keys = Object.keys(resultFromFetch);
console.log(keys);
Prince Hernandez
  • 3,623
  • 1
  • 10
  • 19