0

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);
            };
        };

    });
};
Aᴍɪʀ
  • 7,623
  • 3
  • 38
  • 52
Vince
  • 1,405
  • 2
  • 20
  • 33
  • 2
    JSON is a notation, not a type of object. Those are just "objects", not "JSON objects". – Joe Enos Nov 01 '16 at 20:38
  • Is your data a JSON? If so you have to use `JSON.parse` to get an object out of it. – BrunoLM Nov 01 '16 at 20:41
  • @joe fair comment, any ideas what I need to do ? – Vince Nov 01 '16 at 20:41
  • @BrunoLM yes - it is coming off of Laravel as a json collection – Vince Nov 01 '16 at 20:42
  • Extra whitespace perhaps? Maybe some HTML tags? You didn't show your markup, so who knows. Try `var selected = $(this).text().trim();`. –  Nov 01 '16 at 20:43
  • *"All I get is the `i` value..."* That just doesn't make much sense. I don't see any logging in your code, so we don't know what you're doing for debugging. –  Nov 01 '16 at 20:48
  • `$(this).html()` is very likely to have extra whitespace. Try using `$(this).html().trim()`. – Barmar Nov 01 '16 at 20:48
  • How are you parsing the information from the object? – JDavila Nov 01 '16 at 20:50
  • @JDavila Ummm .... Parsing ? I'm not - I thought I could just access the object like an array. – Vince Nov 01 '16 at 20:56
  • @squint let me clarify: when I run it through the js debugger in Chrome, the variable, **data[i].name** all I see is the **i** value from the For statement. – Vince Nov 01 '16 at 20:58
  • @JDavila - it already is an object .. no need to parse. – Vince Nov 01 '16 at 21:51
  • Why the down vote on my question? – Vince Nov 01 '16 at 21:52
  • @Barmar tried trim - it didn't fix it. – Vince Nov 01 '16 at 21:52
  • @Vince, just thought you could JSON Parse instead, though this is javascript. – JDavila Nov 01 '16 at 21:59
  • Vince, yes parsing. You said you have JSON, which means it needs to be parsed before it's accessed, however the top code block in your question suggests that it's already parsed. There's no actual problem in the code you show, there's no debugging info, and the description doesn't make sense, so that's why there's a down vote on your question. Be happy there aren't more. –  Nov 01 '16 at 23:51

1 Answers1

-1

Here is the answer. Notice that a [0]is placed at the end of the return statement. Credit to, stackoverflow answer here:

                     var result = data.filter(function( obj )
                     { return obj.name == selected; })[0];

                    console.log('line 96', result.lft, result.rgt);
Community
  • 1
  • 1
Vince
  • 1,405
  • 2
  • 20
  • 33
  • It's really super that you put a zero there, but the logic is basically the same as your original code. This doesn't make sense as an answer... and `.filter()` doesn't really make sense if you only want the first result. –  Nov 01 '16 at 23:48
  • @squint Well it works. My original code did not. It delivers the answer to the problem. And that is more than anyone else arrived at. – Vince Nov 02 '16 at 21:06
  • No, as I said, the logic is the same. If it "works", then you didn't represent the actual issue in the question. Nobody would give that answer because it doesn't make any sense that that would work if the code in the question doesn't. –  Nov 02 '16 at 21:09
  • @squint Yeah, sure, great, whatever, you're impressive. Thanks for your help. – Vince Nov 02 '16 at 21:13
  • There's no need to be childish. Just accept it when you're wrong and you may actually learn a thing or two. [Here's a demo](https://jsfiddle.net/v32qjb3a/) that illustrates my point. I don't care what you use; I'm just telling you that your original code will work just as effectively as your "answer" because it's using the same logic to find the match. –  Nov 02 '16 at 21:26
  • @squint Thank you for running up the fiddle. Clearly your example works. I don't understand why my code did not work inside a console.log, but does work when output to a page. Look, there are a lot of people who really really want to help those of us who know less than they do. I would say 90%. When a person posts a question it is certain that they are confused and genuinely do not know what is going on. I am always impressed by those who work with the poster to get to the kernel of their need by asking qualifying questions. Cheers. – Vince Nov 02 '16 at 21:59