Finding a property in a JSON object

I am 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 something in this or that. None of them 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

having looked

typeof(tags.jane);

      

shows it "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 it might tags.jane

possibly 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 creating the JSON object incorrectly? Wrong setting value (s)? Any advice is much appreciated. If it were simpler than an array instead of an object, I have complete control over modifying it. I'm just a little puzzled as to how I should feel about this.

+3


source to share


5 answers


jQuery.inArray returns -1 when no element is found. This value is true

from POV Javascript. Try the following:



if($.inArray('foo', tags.jane) != -1) {

      

+4


source


Your second set of answers is how you should go. However, $ .inArray returns an index, not a boolean. Any non-zero integer is true, which means that when foo

not found, it returns -1, which evaluates to before true

and prints YES .

Likewise, it $.inArray('chicken', tags.jane)

returns 0

and discards false, which is also not the desired answer.



Use $.inArray('foo', tags.jane) !== -1

as your condition instead .

+3


source


tags.name will give you an array for that person. Thus, it $.inArray("chicken",tags.jane)

will see if there is a "chicken" in the jane tag array. If it is not, you will get -1, otherwise you will place it in the array (using your example, this will return zero, the first element of the array).

+2


source


You are using the keyword for the wrong reason. The (prop 'in' obj) assertion checks if the object (associated array) has a prop value. Since you are using the "in" keyword on an array then false is returned, because tags.jane is an array with indices, not a linked array with properties.

If you want to know what the values ​​are in an array then loop through and compare. If you want to use the "in" keyword, then convert your array to such an object.

    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.org/en/JavaScript/Reference/Statements/for...in

+1


source


you cannot use

if('foo' in tags.jane))

      

it should be used like

if (1 in tags.jane)

      

if you want to check 'foo' in tag.jane try

var inIt = (function() {
    var inIt = false;
    tags.jane.forEach(function(item) {
        inIt = inIt || 'foo' == item;
    });
    return inIt;
})();

      

0


source







All Articles