Avatar of MJ
MJ
Flag for United States of America asked on

Efficient Indexing of JSON Object Key in JavaScript

I'm trying to figure out the most efficient way in javascript to take a value (x) and compare it against the key of the JSON object. If the key exists in the object then obviously it returns the value associated with the key. Also if the key doesn't exist, return false. I'm trying to make this as fast but browser friendly as possible.
var botwts={"98B17616F9EFA270B92602BD4AFAD1FA36BB53304A07589536F58CF85B4AC423":true,"7C887696418A36A1F6695280CF1D1E07710751D4A3DF9A4B04B0175FA1F1EE05":true,"B398E318FF11163E8372E0279EB567AF03DB4A10EFABE03689355A295CE9254B":true,"abc":true,"9486A4D194C12E0A6CC3B5464534F0C6E4EF10F85E3BDCD402059B73DEA61344":false}

Open in new window


Thanks In Advance!
JavaScriptJSON

Avatar of undefined
Last Comment
MJ

8/22/2022 - Mon
SOLUTION
leakim971

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
Shaun Vermaak

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Julian Hansen

This question can have two meanings
1. If the value is false or does not exist (also false) it does not matter - set the result to false.
OR
2. You want to know if the key exists - even it if it has a false value

For both you can use the hasOwnProperty() function

There are two ways to get the value or false if the key does not exist. One using hasOwnProperty and the other using the || (OR) operator
var v = botwts['abc'] || false;
var x = botwts['notthere'] || false;
console.log(v,x);
// Alternatively
var x = botwts.hasOwnProperty('abc') ? botwts['abc'] : false;

Open in new window


If the key does not exist - then the x/y variables will get the value false.

If however you want to know if the key exists then
if (botwts.hasOwnProperty('abc')) {
   // do something with the knowledge that the key exists even if it is false
}
else {
  // key does not exist
}

Open in new window

MJ

ASKER
Any issue with this approach ( x = actionHash)? My main concern is speed and efficiency here.
var keyVal = (typeof botwts == "undefined") ? false : botwts[actionHash] || false;

Open in new window


Thanks!
ASKER CERTIFIED SOLUTION
Julian Hansen

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
MJ

ASKER
Thank you all!
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck