I have the following JSON:
[{"active":false,"id":1,"name":"Day"},{"active":true,"id":2,"name":"Evening"},{"active":false,"id":3,"name":"Night"},{"active":false,"id":4,"name":"Away"}]
I'm trying to write JavaScript to find the "name" that is "active" set as "true".
function updateMode() {
var responseString = '';
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
responseString = xhttp.responseText;
var obj = JSON.parse(responseString);
obj.forEach(function(key, index){
console.log(key.active);
console.log(key.name);
alert(index + "." + key.active + "," + key.name);
if (key.active == "true") {
alert("mode is true");
}
});
}
};
xhttp.open("GET", "http://192.168.0.xxx/blahblahblah", true);
xhttp.send();
};
The if (key.active == "true") is not working. How to return the "name" that has "active" true?