简体   繁体   English

在 JSON Object 寻找房产

[英]Finding a property in a JSON Object

I'm creating a JSON object like我正在创建一个 JSON object 之类的

tags = {"jon":["beef","pork"],"jane":["chicken","lamb"]};

which was generated using php from an array like它是使用 php 从类似数组中生成的

$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向控制台写入NO

if('foo' in tags.jane)) {
    console.log('YES');
} else {
    console.log('NO');
}

also writes NO to the console也向控制台写入NO

so looking at所以看着

typeof(tags.jane);

it shows it's an "object" but它显示它是一个"object"但是

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所以我想也许tags.jane实际上可能是一个数组并尝试过

if($.inArray('lamb', tags.jane)) {
    console.log('YES');
} else {
    console.log('NO');
}

which writes YES to the console butYES写入控制台但是

if($.inArray('foo', tags.jane)) {
    console.log('YES');
} else {
    console.log('NO');
}

also writes YES to the console.还将YES写入控制台。

Am I incorrectly building the JSON Object?我是否错误地构建了 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.如果这作为数组而不是 object 会更容易,我可以完全控制更改它。 I'm just a bit stumped at how I should treat this.我只是对如何对待这件事感到困惑。

jQuery.inArray returns -1 when element is not found. jQuery.inArray在找不到元素时返回 -1。 That's true value from the POV of Javascript. Try this:这是 Javascript 的 POV 的true价值。试试这个:

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 .你的第二组答案是你应该 go 的方式。但是,$.inArray 返回一个索引,而不是 boolean。任何非零的 integer 都是真的,这意味着当找不到foo时,它返回 -1 计算结果为true和打印YES

Similarly, $.inArray('chicken', tags.jane) would return 0 and cast to false, which is also not the answer you want.同样, $.inArray('chicken', tags.jane)会返回0并转换为 false,这也不是您想要的答案。

Instead, use $.inArray('foo', tags.jane) !== -1 as your condition.相反,使用$.inArray('foo', tags.jane) !== -1作为您的条件。

tags.name will give you the array for that person. tags.name 会给你那个人的数组。 So $.inArray("chicken",tags.jane) would see if "chicken" is in jane's tags array.所以$.inArray("chicken",tags.jane)会查看“chicken”是否在 jane 的标签数组中。 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).如果不是,您将得到 -1,否则您将在数组中得到 position(使用您的示例,这将返回零,即第一个数组元素)。

You're using the keyword in for the wrong reason.您出于错误的原因使用了关键字 in。 The statement ( prop 'in' obj ) checks to see if the object(associated array) has a property with the value of prop.语句 (prop 'in' obj) 检查对象(关联数组)是否具有值为 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.由于您在数组上使用“in”关键字,因此将返回 false,因为 tags.jane 是一个带有索引的数组,而不是一个带有属性的关联数组。

If you want to know was values are in the array then loop through and compare.如果您想知道数组中是否有值,则循环并比较。 If you want to use the 'in' keyword then convert your array to an object like so.如果你想使用 'in' 关键字,那么将你的数组转换为 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 https://developer.mozilla.org/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如果你想检查 'foo' 在 tags.jane 中,试试这个

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM