• python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Facebook Twitter Instagram
Devs Fixed
  • python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Devs Fixed
Home ยป Resolved: Return the key from a JavaScript array?

Resolved: Return the key from a JavaScript array?

0
By Isaac Tonny on 17/06/2022 Issue
Share
Facebook Twitter LinkedIn

Question:

Using PHP I can return the key by looking up the value inside an array.
But I’m learning javascript, I’ve searched and haven’t found a solution, I’m still a beginner.
How can I return the key by fetching the value within a given array?
I’ve already tried using its functions: .indexOf() or .findIndex()
How to Return ‘car’ if Value = ‘Ferrari’ ?
another doubt in this case is it better to use Array or Class? Is it possible to return the class key?

Answer:

Arrays don’t have keys, only numeric indexes. When you pass a string to an Array, you are actually creating a new property for the Array object, not a new item in the Array data (for example, .length is a property of an Array, not an indexed value).


If you need keys, use an object, which is structured as follows:
where the key is always a string, so quotes around the key name are not necessary.


If you have better answer, please add a comment about this, thank you!

arrays javascript
Share. Facebook Twitter LinkedIn

Related Posts

Resolved: How do I use SetWindowText with Unicode in Win32 using PowerShell?

01/04/2023

Resolved: Shopware 400 Status Error “This value is too long. It should have 255 character or less.” When I Try Updating Database Table

01/04/2023

Resolved: Using AWK to count multiple patterns in a file

01/04/2023

Leave A Reply

© 2023 DEVSFIX.COM

Type above and press Enter to search. Press Esc to cancel.