admin管理员组文章数量:1391929
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
orundefined
, etc. :-) ). – T.J. Crowder Commented Apr 22, 2010 at 16:17
3 Answers
Reset to default 8You 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;
本文标签:
版权声明:本文标题:In JavaScript, how can I return a boolean value indicating whether a key is present in a JSON object? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744098656a2590752.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论