Get value from json array and compare to <td> value

I am trying to match what is in the json string/array to what the td value is.

Here is the jquery i use to compare the data:

$('td.status4').each(function(){
    var tr=$(this).closest('tr');
    if ($(this).text() == '48.4') {
        tr.css('background-color','#f4f1a2');
    }
});

The json data is like so:

Object {71: “48.4”, 72: “48.2”, 73: “48.3”, 74: “48.5”, 75: “48.1”, 76: “54.4”, 77: “54.3”, 78: “54.5”, 79: “54.1”, 80: “54.2”, 81: “67.3”, 82: “67.4”, 83: “67.2”, 84: “67.1”, 85: “49.1”, 86: “49.2”, 87: “49.3”, 88: “49.4”, 89: “55.1”}

What i would like to do is compare the td value in the first code snippet and if there is a match, change the tr background color. I just can’t figure out how to compare the json to the td value.

Thanks for any help!

1 Like

You can iterate over the properties of an object with for...in, like

$('td.status4').each(function() {
  for (var i in object) {
    if (object[i] === $(this).text()) {
      $(this).closest('tr').css('background-color','#f4f1a2');
      break;
    }
  }
});
1 Like

That is awesome… Lesson learned… Thank you very much!

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.