Rorabih Rorabih - 7 months ago 14
Javascript Question

How can I check whether a JSON Object key has a certain value

How can I check whether a JSON object key has a certain value?

I use PHP to fetch data and json_encode to get the output in JSON. Afterwards I use Ajax to append the result into a div.

What I'm trying to achieve is if a certain key has a certain value - then do group them together: something like this:

If Key_Status = "value_Solved" {
// do something
} else If Key_Status = "value_Not_Solved" { {
// do something else
};


I do not receive any errors nor output.

This is what I have achieved so far:

$.ajax({
url: "api-json.php?api",
data: {id: idGet},
dataType: "text",
success: function(data) {
var json = $.parseJSON(data);
if (json) {
$.each(json, function(idx, obj) {
//now json variable contains data in obj format
//let's display a few items
var id = obj.task.id;
var customer = obj.task.customer;
var task = obj.task.task;
var status = function(key, val) {
var elm = obj.task.status;
if ( elm == 'Solved' ) {
$("#"+id).css("background", "blue");
console.log("#"+id);
}
};
var solver = obj.task.solver;
var issuer = obj.task.issuer;
var added = obj.task.added;
var label = obj.task.label;

$('#results')
.append(" <div class='board-list-box' id='" + id + "'> <a class='board-list-box-edit'><span class='edit'></span><span class='update'></span><span class='delete'></span><span class='screen-reader-text'>Edit</span></a> <label class='board-list-box-label " + label + "'></label> <p class='board-list-box-text'> " + task + "</p> <span class='board-list-box-info'>" + added + "</span> </div>");
});
} else {
$('#results').html("<p class='error'>" + json + " tasks to do </p>");
}
}
});


My JSON OUTPUT is like this:

[
{
"task": {
"id": "537",
"task": "Test 1",
"description": "",
"customer": "",
"issuer": "",
"solver": "",
"label": "green",
"status": "Not started",
"deadline": "0000-00-00 00:00:00",
"added": "16-01-24 06:20:55"
},
"comments": "comments",
"labels": "labels"
},
{
"task": {
"id": "538",
"task": "Test 2",
"description": "",
"customer": "",
"issuer": "",
"solver": "",
"label": "green",
"status": "Not started",
"deadline": "0000-00-00 00:00:00",
"added": "16-01-24 06:20:58"
},
"comments": "comments",
"labels": "labels"
}


]

Answer

Edited Answer:

Let's refer the below method:

function CheckTasks() {
  var Tasks = '[{ "task": { "id": "537", "task": "Test 1", "description": "", "customer": "", "issuer": "", "solver": "", "label": "green", "status": "Not started", "deadline": "0000-00-00 00:00:00", "added": "16-01-24 06:20:55" }, "comments": "comments", "labels": "labels" }, { "task": { "id": "538", "task": "Test 2", "description": "", "customer": "", "issuer": "", "solver": "", "label": "green", "status": "Not started", "deadline": "0000-00-00 00:00:00", "added": "16-01-24 06:20:58" }, "comments": "comments", "labels": "labels" }]';

  var obj = JSON.parse(Tasks);
  var groupedTasks=[];  // Array which will have the Task IDs if the Status is Not started.

  for (var key in obj) {
    if (obj.hasOwnProperty(key)) {
      //alert(obj[key].task.status); // To access the Status of a Task

      if (obj[key].task.status == "Not started") {
        groupedTasks.push(obj[key].task.id);  // Push the Task ID in the array.
      }
    }
  }
  alert(groupedTasks) ;  // The Grouped Tasks with Status as Not started will be available in this Array.
}

The JSON.parse() method parses a string as JSON.

key refers to the number of tasks present. We have 2 tasks(537 & 538).

The hasOwnProperty() method returns a boolean indicating whether the object has the specified property.

In the above code, we iterate through each key (tasks) and check whether it has a property or not. If it has a property, then get the value using obj[key].task.status.

An array groupedTasks is created and if the status is Not started, the IDs of those tasks are added(pushed) into this array.

Please refer this JSFiddle link and check the results.