I am struggling to search through an array of objects and find a specific value.
I have an array of json objects like this:
Object {category_id: "2", name: "MATERIALS", lft: "2", rgt: "17", created_at: "-0001-11-30 00:00:00"…}
Object {category_id: "3", name: "underlayments", lft: "5", rgt: "6", created_at: "-0001-11-30 00:00:00"…}
I have a page with all the major categories. Suppose I have clicked on the major category "MATERIALS". Now I want to compare what I have clicked with the values in my array such that the selected value "MATERIALS" equals the "name:MATERIALS" , from there, I can pull out lft
and rgt
values for "Materials". So I do this, But I am not getting the name value when "selected" and the object are matched. All I get is the i
value, and therefore my if statement does not succeed.
My guess is my syntax is wrong.
function receiver(data, textStatus, XMLHttpRequest) { //COMES FROM AN AJAX QUERY
$("td.depthOne").click(function () {
var selected = $(this).html(); // THIS WORKS
var i;
for (i = 0; i < data.length; i++) {
if (data[i].name == selected) { //PROBLEM: THIS DOES NOT WORK - I DON'T GET THE NAME OUT OF THE OBJECT, ONLY THE "i" VALUE
lft = data[i].lft;
rgt = data[i].rgt;
catPosition(lft, rgt);
};
};
});
};