I'm creating a JSON object like
tags = {"jon":["beef","pork"],"jane":["chicken","lamb"]};
which was generated using php from an array like
$arr = array(
'jon' => array('beef', 'pork'),
'jane' => array('chicken', 'lamb')
);
$tags = json_encode($arr);
And I want to check if something is in one or the other. None of these seem to work, but something like
if('lamb' in tags.jane)) {
console.log('YES');
} else {
console.log('NO');
}
writes NO to the console
if('foo' in tags.jane)) {
console.log('YES');
} else {
console.log('NO');
}
also writes NO to the console
so looking at
typeof(tags.jane);
it shows it's an "object"
but
console.log(tags);
shows the following:
Object
jane: Array[2]
0: "chicken"
1: "lamb"
length: 2
__proto__: Array[0]
jon: Array[2]
0: "beef"
1: "pork"
length: 2
__proto__: Array[0]
__proto__: Object
so i thought maybe tags.jane
may actually be an array and tried
if($.inArray('lamb', tags.jane)) {
console.log('YES');
} else {
console.log('NO');
}
which writes YES to the console but
if($.inArray('foo', tags.jane)) {
console.log('YES');
} else {
console.log('NO');
}
also writes YES to the console.
Am I incorrectly building the JSON Object? Not targeting the value(s) properly? Any advice is greatly appreciated. If this would be easier as an array instead of an object, I have full control to change it. I'm just a bit stumped at how I should treat this.
I'm creating a JSON object like
tags = {"jon":["beef","pork"],"jane":["chicken","lamb"]};
which was generated using php from an array like
$arr = array(
'jon' => array('beef', 'pork'),
'jane' => array('chicken', 'lamb')
);
$tags = json_encode($arr);
And I want to check if something is in one or the other. None of these seem to work, but something like
if('lamb' in tags.jane)) {
console.log('YES');
} else {
console.log('NO');
}
writes NO to the console
if('foo' in tags.jane)) {
console.log('YES');
} else {
console.log('NO');
}
also writes NO to the console
so looking at
typeof(tags.jane);
it shows it's an "object"
but
console.log(tags);
shows the following:
Object
jane: Array[2]
0: "chicken"
1: "lamb"
length: 2
__proto__: Array[0]
jon: Array[2]
0: "beef"
1: "pork"
length: 2
__proto__: Array[0]
__proto__: Object
so i thought maybe tags.jane
may actually be an array and tried
if($.inArray('lamb', tags.jane)) {
console.log('YES');
} else {
console.log('NO');
}
which writes YES to the console but
if($.inArray('foo', tags.jane)) {
console.log('YES');
} else {
console.log('NO');
}
also writes YES to the console.
Am I incorrectly building the JSON Object? Not targeting the value(s) properly? Any advice is greatly appreciated. If this would be easier as an array instead of an object, I have full control to change it. I'm just a bit stumped at how I should treat this.
Share Improve this question asked Apr 6, 2012 at 18:31 ocergynohtnaocergynohtna 1,7022 gold badges21 silver badges29 bronze badges 6- use JSON.parse( jsonString ) to convert the server response to a javascript object. – Larry Battle Commented Apr 6, 2012 at 18:34
- 2 +1 for a thorough demonstration of what you tried. – user1106925 Commented Apr 6, 2012 at 18:41
- sorry i didn't mention i was parsing the JSON in the js, but either way Sergio (and others) gave me what I needed. Thanks all! Out of curiosity, why are tags.jane and tags.jon arrays instead of "nested objects" is that just not possible in JSON? when i started this i was thinking i'd do a check like if(tags.jane.chicken) expecting it to return true or false if the property existed. i obviously need to do some reading on JSON objects. :P Thanks again to all those that chimed in. – ocergynohtna Commented Apr 6, 2012 at 18:49
- 1 @amnotiam: if only there were more questions like this :) – Sergio Tulentsev Commented Apr 6, 2012 at 19:17
-
1
@SergioTulentsev: Agreed. @@ocergynohtna: JSON has 2 data structures. One is an ordered list (an Array) where the keys are excluded in the markup, because they're implicitly 0-based indices. The other is an collection of key/value pairs, where the keys can be any string value. To get the
tags.jane.chicken
behavior, you'd need to usechicken
as a key, and choosetrue
(or another truthy value) as the value, and use the{}
syntax for object notation.{"jon":{"beef":true,"pork":true},"jane":{"chicken":true,"lamb":true}}
– user1106925 Commented Apr 6, 2012 at 19:26
5 Answers
Reset to default 4jQuery.inArray returns -1 when element is not found. That's true
value from the POV of Javascript. Try this:
if($.inArray('foo', tags.jane) != -1) {
Your second set of answers are the way you should go. However, $.inArray returns an index, not a boolean. Any non-zero integer is true, which means when foo
is not found, it returns -1 which evaluates to true
and prints YES.
Similarly, $.inArray('chicken', tags.jane)
would return 0
and cast to false, which is also not the answer you want.
Instead, use $.inArray('foo', tags.jane) !== -1
as your condition.
tags.name will give you the array for that person. So $.inArray("chicken",tags.jane)
would see if "chicken" is in jane's tags array. If it's not, you'd get -1, otherwise you'd it's position in the array (using your example, this would return zero, the first array element).
You're using the keyword in for the wrong reason. The statement ( prop 'in' obj ) checks to see if the object(associated array) has a property with the value of prop. Since you're using the 'in' keyword on an array, then false is going to be returned because tags.jane is an array with indexes and not an associated array with properties.
If you want to know was values are in the array then loop through and pare. If you want to use the 'in' keyword then convert your array to an object like so.
tags = {};
// old code
tags.jane = ['lamb', 'food'];
console.log(('lamb' in tags.jane) === false )
// new code
tags.jane = {
'lamb':1,
'food':1
}
console.log(('lamb' in tags.jane) === true )
https://developer.mozilla/en/JavaScript/Reference/Statements/for...in
you can not use
if('foo' in tags.jane))
it should be used as
if (1 in tags.jane)
if you want to check 'foo' is in tags.jane, try this
var inIt = (function() {
var inIt = false;
tags.jane.forEach(function(item) {
inIt = inIt || 'foo' == item;
});
return inIt;
})();