floober
floober

Reputation: 23

Get a specific key in a javascript object fast

Currently this is my code for getting the key that I want from my Object:

for (let [key, value] of Object.entries(d)) {
   if (key == input){
      //do something
   }
}

Is there a faster way to get that key without iterating through every entry? Thank you

Upvotes: 0

Views: 56

Answers (2)

CityNIGHT
CityNIGHT

Reputation: 26

Find will find and return first element founded, so theoretically if you element lays first it will be blazing fast

Object.keys(obj).find(key => obj[key] === value)

Upvotes: 0

AKX
AKX

Reputation: 169062

You're probably looking for:

d[input]

Upvotes: 2

Related Questions