I’m doing pretty simple thing here: getting Facebook news feed using their graph API. I’m receiving the json object and fetching it to the page. “Likes” come as an array of JSONs (obviously) like so:
data.likes:
{
"count":2,
"data":[
{"name":"Jennifer Doe","id":"{user_id}"},
{"name":"John Doe","id":"{user_id}"}
]
}
My goal is to know if the current user liked it or not – assume that I have current user’s user_id. So, what’s the simplest way to check if the current user is liked this post or not?
I know I can set up for loop and go through each data, but that’s not only inefficient, but also looks bad. I can also use $.inArray() and look for particular JSON object, but I want to be able to search by user_id only.
Any help would be appreciated. Thanks!
Two solutions come to my mind immediately:
-a for loop like this
-you could alternatively convert the JSON object in a string with
and then search in it for the user id with some search method (for example ‘search’: http://www.w3schools.com/jsref/jsref_search.asp or also indexOf: http://www.w3schools.com/jsref/jsref_indexof.asp).
if you need more details I can elaborate on this.