检查JSON对象中是否存在值

时间:2021-07-09 12:14:10

I have the next JSON:

我有下一个JSON:

var JSONObject = {"animals": [{name:"cat"}, {name:"dog"}]};

What is the best way to know if the "dog" value exists in the JSON object?

知道“dog”值是否存在于JSON对象的最佳方法是什么?

Thanks.

谢谢。

Solution 1

解决方案1

var JSONObject = {"animals": [{name:"cat"}, {name:"dog"}]};
...
for (i=0; i < JSONObject.animals.length; i++) {
    if (JSONObject.animals[i].name == "dog")
        return true;
}
return false;

Solution 2 (JQuery)

解决方案2(JQuery)

var JSONObject = {"animals": [{name:"cat"}, {name:"dog"}]};
...
$.map(JSONObject.animals, function(elem, index) {
 if (elem.name == "dog") 
     return true;
});
return false;

Solution 3 (using some() method)

解决方案3(使用some()方法)

function _isContains(json, value) {
    let contains = false;
    Object.keys(json).some(key => {
        contains = typeof json[key] === 'object' ? 
        _isContains(json[key], value) : json[key] === value;
        return contains;
    });
    return contains;
}

5 个解决方案

#1


14  

var JSON = [{"name":"cat"}, {"name":"dog"}];

The JSON variable refers to an array of object with one property called "name". I don't know of the best way but this is what I do?

JSON变量指的是一个对象数组,其中包含一个名为“name”的属性。我不知道最好的办法,但我就是这么做的?

var hasMatch =false;

for (var index = 0; index < JSON.length; ++index) {

 var animal = JSON[index];

 if(animal.Name == "dog"){
   hasMatch = true;
   break;
 }
}

#2


3  

Below function can be used to check for a value in any level in a JSON

下面的函数可以用来检查JSON中任何级别的值

function _isContains(json, value) {
    let contains = false;
    Object.keys(json).some(key => {
        contains = typeof json[key] === 'object' ? _isContains(json[key], value) : json[key] === value;
         return contains;
    });
    return contains;
 }

then to check if JSON contains the value

然后检查JSON是否包含该值

_isContains(JSONObject, "dog")

See this fiddle: https://jsfiddle.net/ponmudi/uykaacLw/

看到这个小提琴:https://jsfiddle.net/ponmudi/uykaacLw/

Most of the answers mentioned here compares by 'name' key. But no need to care about the key, can just checks if JSON contains the given value. So that the function can be used to find any value irrespective of the key.

这里提到的大多数答案都用“name”键进行比较。但是不需要关心键,只需检查JSON是否包含给定的值。所以这个函数可以被用来找到任何值,不管键是什么。

#3


2  

Check for a value single level

const hasValue = Object.values(json).includes("bar");

Check for a value multi-level

function hasValueDeep(json, findValue) {
    const values = Object.values(json);
    let hasValue = values.includes(findValue);
    values.forEach(function(value) {
        if (typeof value === "object") {
            hasValue = hasValue || hasValueDeep(value, findValue);
        }
    })
    return hasValue;
}

#4


0  

This example puts your JSON into proper format and does an existence check. I use jquery for convenience.

这个示例将JSON放入适当的格式并进行存在检查。我使用jquery是为了方便。

http://jsfiddle.net/nXFxC/

http://jsfiddle.net/nXFxC/

<!-- HTML -->
<span id="test">Hello</span><br>
<span id="test2">Hello</span>

//Javascript

$(document).ready(function(){
    var JSON = {"animals":[{"name":"cat"}, {"name":"dog"}]};

if(JSON.animals[1].name){      
$("#test").html("It exists");
}
if(!JSON.animals[2]){       
$("#test2").html("It doesn't exist");
}
});

#5


-4  

iterate through the array and check its name value.

遍历数组并检查其名称值。

#1


14  

var JSON = [{"name":"cat"}, {"name":"dog"}];

The JSON variable refers to an array of object with one property called "name". I don't know of the best way but this is what I do?

JSON变量指的是一个对象数组,其中包含一个名为“name”的属性。我不知道最好的办法,但我就是这么做的?

var hasMatch =false;

for (var index = 0; index < JSON.length; ++index) {

 var animal = JSON[index];

 if(animal.Name == "dog"){
   hasMatch = true;
   break;
 }
}

#2


3  

Below function can be used to check for a value in any level in a JSON

下面的函数可以用来检查JSON中任何级别的值

function _isContains(json, value) {
    let contains = false;
    Object.keys(json).some(key => {
        contains = typeof json[key] === 'object' ? _isContains(json[key], value) : json[key] === value;
         return contains;
    });
    return contains;
 }

then to check if JSON contains the value

然后检查JSON是否包含该值

_isContains(JSONObject, "dog")

See this fiddle: https://jsfiddle.net/ponmudi/uykaacLw/

看到这个小提琴:https://jsfiddle.net/ponmudi/uykaacLw/

Most of the answers mentioned here compares by 'name' key. But no need to care about the key, can just checks if JSON contains the given value. So that the function can be used to find any value irrespective of the key.

这里提到的大多数答案都用“name”键进行比较。但是不需要关心键,只需检查JSON是否包含给定的值。所以这个函数可以被用来找到任何值,不管键是什么。

#3


2  

Check for a value single level

const hasValue = Object.values(json).includes("bar");

Check for a value multi-level

function hasValueDeep(json, findValue) {
    const values = Object.values(json);
    let hasValue = values.includes(findValue);
    values.forEach(function(value) {
        if (typeof value === "object") {
            hasValue = hasValue || hasValueDeep(value, findValue);
        }
    })
    return hasValue;
}

#4


0  

This example puts your JSON into proper format and does an existence check. I use jquery for convenience.

这个示例将JSON放入适当的格式并进行存在检查。我使用jquery是为了方便。

http://jsfiddle.net/nXFxC/

http://jsfiddle.net/nXFxC/

<!-- HTML -->
<span id="test">Hello</span><br>
<span id="test2">Hello</span>

//Javascript

$(document).ready(function(){
    var JSON = {"animals":[{"name":"cat"}, {"name":"dog"}]};

if(JSON.animals[1].name){      
$("#test").html("It exists");
}
if(!JSON.animals[2]){       
$("#test2").html("It doesn't exist");
}
});

#5


-4  

iterate through the array and check its name value.

遍历数组并检查其名称值。