0

I'm really sorry to be posting all day. I'm a total newbie with this coding. Basically, with the help of a lot of amazing people here, I'm setting up a JSON thingy for the first time. I'm trying to evaluate a list against an input. Here's sample data:

    {
    "

films": [{
        "label": "34",
        "value": "34",
        "currently_streaming": "1",
        "full_streaming_url": "http://www.url.com",
        "url": "http://www.url.com"},
    {
        "label": "A Different Color",
        "value": "A Different Color",
        "currently_streaming": "1",
        "full_streaming_url": "http://www.url.php",
        "url": "http://www.url.com"}]
}​

and here's my code. I finally got it, (thank you everyone!) to select the value portion that I wanted. I then tried to compare it in an if/else statement, and it's doing weird things. First, it seems to be pulling more values than actually exist on the file, but I'm not completely sure about that. Secondly, however, it seems to be not comparing values, but setting one the value I'm iterating through equal to another value! I don't understand!

 var test = "34x25x36";
 $(document).ready(function () {
     $.ajax({
         dataType: 'json',
         beforeSend: function () {
             console.log('Before Ajax Request Starts !!');
         },
         success: function (data) {
             console.log(data);
             alert("Edddddddd");
             $.each(data.films, function (i, object) {
                 $.each(object, function (property, value) {
                     //alert(property + "=" + value);
                     for (i = 0; i < data.films.length; i++) {
                         var theFilm = (data.films[i].value);
                         if (theFilm == test) {
                             document.write(theFilm);
                             document.write(test + "<br>");
                         } else {}
                     }
                 });
             });
         },
         error: function (jqXHR, textStatus, errorThrown) {
             alert("Error occurred: " + errorThrown);
         },
         beforeSend: function () {
             console.log('Ajax Request Complete  !!');
         },
         url: 'test.php'
     });
 });

EDIT

When I do have something in the else{ } section, it seems to run the whole thing multiple times, judging correctly when doesn't match, but then it seems to run again, running the text for the one match and then a bunch of "not match" text. When there is nothing in the else{} section, it seems to set the value of theFilm = test . Help!

3
  • 1
    You can ask as many questions as you want here, as long as they follow the guidelines and faq. :) Commented Sep 21, 2012 at 20:08
  • Don't use document.write - set up a DIV and set the innerHTML. Commented Sep 21, 2012 at 20:09
  • Well, from what I can see, you have two line breaks after the first quotation mark ". I don't know if this would work in JSON but it wouldn't in regular JS. Commented Sep 21, 2012 at 20:12

2 Answers 2

1

your success function is messed up.. Look's like you are getting confused by using many loops..

Also do not write it to a document.. Use console.log or alert to debug..

Try this approach

success: function(data) {
    console.log(data);
    alert("Edddddddd");
    var keys = ["label", "value", "currently_streaming", "full_streaming_url", "url"]
    for (i = 0; i < data.films.length; i++) {
        for (j = 0; j < keys.length; j++) {
            // alert('Key - ' + keys[j] + ' :: Value - ' + data.films[i][keys[j]]);
            var theFilm = data.films[i][keys[j]];
            if (theFilm == test) {
                alert('TheFilm Variable is : ' + theFilm);
                alert('Test Variable is : ' + test );
            }
            else {
               alert('No Match Found !!');
            }
        }
    }
}​
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! I couldn't get that to work - is it worth knowing that I only want to compare value? so the value of test is only a value of a value key.
I don't think I understand you.. can you be a bit more specific
0

You are looping through all films, and then inside that you are looping through all properties of each film. Then inside that you are looping through all films again. That's a lot of looping for no reason. If you have for example 100 films with 5 properties in each, you will be looping through all the films 500 times, thus writing out the film that you find 500 times.

You only need one loop to loop through the films and find one:

$.each(data.films, function(i, object) {
  var theFilm = object.value;
  if (theFilm == test) {
    document.write(theFilm);
  }
});

Demo: http://jsfiddle.net/baJtf/

You can also use the grep method to find items in an array:

var film = $.grep(data.films, function(f){
  return f.value == test;
});
if (film.length == 1) {
  document.write(film[0].value);
}

Demo: http://jsfiddle.net/baJtf/1/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.