Languages

JavaScript - get element from object by index

0 points
Asked by:
chelsea
806

How to get element from object by index?

Let's say I have the following object:

and I need to get the first element of it.

How can I do something like myObject[0] to get the 'aaa' value?

1 answer
0 points
Answered by:
Admin Dirask Community
4380

You can use Object.keys() method to get array of the object's keys and then using array notation ([]) you can access them by index like:

 

Practical example

Note:

Because you have arrays under a and b key, the aValues and bValues will also be arrays. Going further, you may access individual elements like:

 

References

  1. Object.keys() - JavaScript | MDN
0 commentsAdd comment
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join