Check whether a value exists in JSON object
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;
}
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?
var hasMatch =false;
for (var index = 0; index < JSON.length; ++index) {
var animal = JSON[index];
if(animal.Name == "dog"){
hasMatch = true;
break;
}
}