0

I used

console.log(response);

and got the following in console (in Firebug). How do I loop through to check if "LMNOPQ" exists?

data        [Object { name="Excel in ABCD", category="Book", id="327361517327399", more...}, Object { name="LMNOPQ", category="Product/service", id="175625882542984", more...}, Object { name="UVWXYZ", category="Book", id="260641707360118", more...}, 7 more...]
            0       Object { name="Excel in ABCD", category="Book", id="327361517327399", more...}
            category        "Book"
            created_time        "2012-04-04T05:31:04+0000"
            id      "327361517327399"
            name        "Excel in ABCD"
            1       Object { name="LMNOPQ", category="Product/service", id="175625882542984", more...}
            2       
Object { name="UVWXYZ", category="Book", id="260641707360118", more...}

Then as suggested by Baptiste Pernet, I tried the following:

for(var i in response) {
    console.log(response[i].name);//gives me undefined
    console.log(response[i]);//gives me another object (it is nested, check below)
}

[Object { name="Excel in ABCD", category="Book", id="327361517327399", more...}, Object { name="LMNOPQ", category="Product/service", id="175625882542984", more...}, Object { name="UVWXYZ", category="Book", id="260641707360118", more...},

How do I get this name now? I'm stuck at:

console.log(response[i]);

What should I write in order to get the properties of nested objects? Another loop?

2
  • You may Use jsonpath library Commented Apr 5, 2012 at 7:47
  • Give us the url of the jsonp please Commented Apr 5, 2012 at 8:14

2 Answers 2

2

You should try to use JSON.stringify(response) and then this site to visualize the result. It is far more standard than the format you provide.

From what you gave, it seems that you have a member called data that contains an array (again, I am not sure because you didn't provide a good format to describe you javascript object).

So let's try

for(var i in response.data) {
  if (response.data[i].name == 'LMOPQ') {
    return true
  }
}

return false;
Sign up to request clarification or add additional context in comments.

8 Comments

nice.... but isnt it a strange syntax ? data [Object { what does the Object word doing there ?
I think this is the FF console log, it says that it is an array of object data[Object{...}, Object{...}, ...]
yes Baptiste Pernet, its FF console log. I'll try this and get back
well, I used like, because you wrote 'like' in your question... in console.log(response[like]);
Apologies, thats my mistake, it was just console.log(response);
|
-1

you can use

for (var prop in Object) {
    if(prop == "LMNOPQ") {
        // Do something
    }
}

or you can use if( response[like].hasOwnProperty("LMNOPQ") ) alternatively.

3 Comments

Well, I'm fetching facebook likes by a user who has authenticated us already and checking if my page exists in it.
FB.api('/me/likes, function(response) { for(var i in response[like]) { console.log(response[like][i].name); } });
extremely sorry for the confusion, it was just console.log(response); that logged everything I mentioned in my question, originally.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.