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?

10
  • 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) to get an array of keys. Commented Sep 15, 2020 at 5: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. Commented Sep 15, 2020 at 5:44
  • @GirkovArpa OP talks about JSON Objects, which makes no sense. It's also not JSON, it's just an object literal. Commented Sep 15, 2020 at 5:45
  • 2
    @user2426691 W3 schools is notoriously misleading and plain wrong in many cases, this one included. Commented Sep 15, 2020 at 5: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 Commented Sep 15, 2020 at 5:47

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

Comments

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); });

Comments

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

Comments

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']; 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.