0

I have this following object:

{
  12232: [],
  34232: [],
  23435: []
}

I want to extract the keys from this object to show them as labels in my React frontend. How can I accomplish this?

GirkovArpa
  • 4,427
  • 4
  • 14
  • 43
  • 3
    There's no such thing as a JSON Object. JSON is a text format. What you have there is an object literal, and you can use [`Object.keys(theObject)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys) to get an array of keys. –  Sep 15 '20 at 05:40
  • 1
    "JavaScript Object Notation Object." If there's something wrong with this, I don't see it. I would say `{ foo: 'bar' }` is a JSO but not a JSON, while `{ "foo": "bar" }` is both. – GirkovArpa Sep 15 '20 at 05:44
  • @GirkovArpa OP talks about JSON Objects, which makes no sense. It's also not JSON, it's just an object literal. –  Sep 15 '20 at 05:45
  • @ChrisG https://www.w3schools.com/js/js_json_objects.asp – user2426691 Sep 15 '20 at 05:45
  • @GirkovArpa "JSO" is not an acronym in widespread use. And there are no "JSON objects" - `a = { "foo": "bar" }` is an object literal in code. There is no special name for that or difference with `a = { foo: "bar" }`. If it's *text*, then `{ "foo": "bar" }` is JSON but it's not JS code. – VLAZ Sep 15 '20 at 05:46
  • @user2426691 No, sorry, that's actually the best example of why w3schools is bad and shouldn't be used, much less linked to from this website. Please remove the comment. See also here: https://meta.stackoverflow.com/questions/280478/why-not-w3schools-com –  Sep 15 '20 at 05:46
  • 2
    @user2426691 W3 schools is notoriously misleading and plain wrong in many cases, this one included. – VLAZ Sep 15 '20 at 05:47
  • 2
    @ChrisG Ok thanks for the heads up, i'll delete the entire question while I'm at it as it is a duplicate – user2426691 Sep 15 '20 at 05:47
  • Sorry guys, it says I can't delete because others have invested time and effort answering – user2426691 Sep 15 '20 at 05:51
  • @VLAZ The following page uses the term "JSON objects": https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/JSON I'm not denying OP misused the term. But I do think it's a legitimate term. Particularly since JSON, taken literally, refers to the syntax. Hence the "Object" in "JSON Object". – GirkovArpa Sep 15 '20 at 05:53

4 Answers4

1

The method Object.keys() allows you to loop over the keys of an object. You should be able to use it in order to get what you want.

See here

ArnaudV
  • 342
  • 2
  • 9
1

Use JS built-in Object.keys() for that.

const myObject = {
  12232: [],
  34232: [],
  23435: []
};


const keys = Object.keys(myObject);

keys.forEach(key => {
  console.log(key);
});
Ahmad
  • 12,336
  • 6
  • 48
  • 88
1

You can always use

  obj = { abc: "1",  def: "2" }
  console.log(Object.keys(obj))

It will log ["abc", "def"]. Then you can map through this array and display the values as required.

Ref

maya_nk99
  • 502
  • 3
  • 5
0

You could use Object.keys to extract the keys out of your object.

You could check the docs of Object.keys here

const obj = {
 12232 : [],
 34232 : [],
 23435: []
}

Object.keys(obj); // returns ['12232', '34232', '23435'];
Prateek Thapa
  • 4,829
  • 1
  • 9
  • 23