In JavaScript, how can I return a boolean value indicating whether a key is present in a JSON object? - Stack Overflow

admin2025-04-08  1

I have a fairly simple question: In Javascript how can I return the boolean (if its found in the JSON) rather than the actual value?

Example:

 var myJSON = { 
                  "foo" : "bar",
                  "bar" : "foo"
              };
 var keyToLookFor = "foo";
 var found = myJSON[keyToLookFor];

 if (found) {
     // I know I can test if the string exists in the if
 }
  // but is there a way I could just return something like:
 return found;
 // instead of testing found in the if statement and then returning true?

I have a fairly simple question: In Javascript how can I return the boolean (if its found in the JSON) rather than the actual value?

Example:

 var myJSON = { 
                  "foo" : "bar",
                  "bar" : "foo"
              };
 var keyToLookFor = "foo";
 var found = myJSON[keyToLookFor];

 if (found) {
     // I know I can test if the string exists in the if
 }
  // but is there a way I could just return something like:
 return found;
 // instead of testing found in the if statement and then returning true?
Share Improve this question edited Apr 22, 2010 at 15:56 Paul D. Waite 99k57 gold badges203 silver badges271 bronze badges asked Apr 22, 2010 at 15:46 AlexAlex 66.1k49 gold badges153 silver badges181 bronze badges 2
  • this may seem odd, but I have a situation where I just need to return whether or not the string exists in the JSON. Not the actual value of the key. – Alex Commented Apr 22, 2010 at 15:48
  • Not odd at all, that sort of thing es up all the time -- needing to test for whether the property exists, not needing to find its value (which would be tricky in the above if its value is false or undefined, etc. :-) ). – T.J. Crowder Commented Apr 22, 2010 at 16:17
Add a ment  | 

3 Answers 3

Reset to default 8

You have to check with the 'in' keyword:

if (keyToLookFor in myJSON) {
}

So to simplify it, you can use:

return keyToLookFor in myJSON;

The in operator can tell you whether a key exists in an object or any of the objects in its prototype chain. The hasOwnProperty function can tell you whether that key exists in the object itself (not in any of the objects on its prototype chain).

if ("foo" in obj) {
   // `obj` or an ancestor has a key called "foo"
}

if (obj.hasOwnProperty("foo")) {
   // `obj` has a key called "foo"
}

In your example, it wouldn't really matter which one you used because your object is just an Object ({} => new Object()), so it doesn't have much of a prototype chain. But it can be relevant, for example, if you're looking at custom properties you've added to other objects, like:

var a = [1, 2, 3, 4];
a.foo = "testing";

alert("push" in a);              // 1. alerts "true"
alert(a.hasOwnProperty("push")); // 2. alerts "false"
alert("foo" in a);               // 3. alerts "true"
alert(a.hasOwnProperty("foo"));  // 4. alerts "true"

#1 above alerts true because all arrays inherit the push property from Array.prototype (it refers to a function that pushes a value onto the end of the array). #2 alerts false because the push property is from the prototype, not actually on the object itself. #3 and #4 alert true because the foo property exists on the object itself.

Side note

What you have there isn't JSON, it's Javascript Object Literal Notation, of which JSON is a subset. You have used a literal (just like a "string literal") to create an object and have assigned it to a variable. I only mention this because object literals can have more in them (including functions) and have a wider syntax. The part within the {} in your example is valid JSON, though, since you used double quotes on both the keys and the values, didn't include "undefined" anywhere, and didn't include any functions. :-)

In your example, if the value of the property is false, found will be false.. even though it was found, so you won't know it's there :)

If you want to continue using your example (with the above mentioned issue), you can use a little trick:

return !!found;

Which will convert found to a boolean.

I would choose to do:

return keyToLookFor in myJSON;
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1744098679a231282.html

最新回复(0)