views:

33

answers:

2

I was given this code earlier but am having a hard time parsing the correct data.

I have the following JSON

{ flavors: [{"image":"images/bbtv.jpg", "desc":"BioBusiness.TV", "id":"1"},{"image":"images/grow.jpg", "desc":"Grow Staffing", "id":"2"}]}

and I want to only show id:1 or id:2.

I have the following code for Ajax

$.ajax({
        type: "POST",
        url: "foodservice/all.js",
        dataType: "json",
        cache: false,
        data: {"flavors": filterId(flavors, 'a')},
        contentType: "application/json",
        success: function(data) {
           $('#flavor-detail').html("<div/>");
           $.each(data.flavors, function(i,item){
              $('#flavor-detail div').append('<ul><li><p>' + item.desc + '</p></li></ul>');
           });
        }         
     });

and the following function to filter through the JSON object

function filterId(obj, filteredId) {
 var resultObj = $.extend({},obj);

 for (var i in obj) {
  if ( obj.hasOwnProperty(i) ) {
   if ( obj[i].id && obj[i].id !== filteredId ) {
    delete obj[i];
   }
  }
 }
 return resultObj;
}

However, this code does not return anything. Can someone tell me what I am missing?

Im pretty new to JSON, Ajax so any help would be greatly appreciated.

Thanks!

+1  A: 

Why not just check in the "each" code?

  $.each(data.flavors, function(i,item){
      if (item.id > 2) return;
      $('#flavor-detail div').append('<ul><li><p>' + item.desc + '</p></li></ul>');
  });
Pointy
that works great for parsing just a simple data, but if i wanted to say for example have <a href="">id1</a> show id1 and <a href="">id2</a> show id2, what would be the best approach?btw, thanks for your answer :)
Hyung Suh
Well, I don't fully understand where you are using that "filter" function, or how you are calling it. It doesn't look wrong, but I don't think I would do it that way; I would be more likely to have a function just find the elements with the "id" values I wanted, and then build a new object to collect those.
Pointy
Oh wait; sorry I must be blind - I see the function now. Let me think for a minute.
Pointy
i had a quick question.when i do item.id == 1, i would expect the output to be biobusiness, but it's returning growstaffing.and vice versa... any reason why this is happening?
Hyung Suh
OK, I still do not understand. The "data" parameter is for sending data *to* your server - in other words, it is for the parameters to the HTTP request. Do you want to filter values going *to* the server, or coming *from* the server? Also, I don't see why you pass 'a' to "filterId", since it seems to look for a match to "id" attributes of the data, and those look like numbers, not letters.
Pointy
im trying to filter values coming from the server.also, you're right, the pass should be a number, not a letter,but it still won't correctly show the data?
Hyung Suh
I made the item.id == 1 show the correct data by doingitem.id != 1wonder why it works that way...
Hyung Suh
A: 

i changed the following code to make it "filterable"

$.ajax({
        type: "GET",
        url: "foodservice/all.js",
        dataType: "json",
        cache: false,
        contentType: "application/json",
        success: function(data) {
           $('#flavor-detail').html("<div/>");
           $.each(data.flavors, function(i,item){
              if (item.id != flavorid) return;
              $('#flavor-detail div').append('<ul><li><p>' + item.desc + '</p></li></ul>');
           });
        }         
     });

and for each link to change the output,

$('#amare').click(function(){
      flavorid = "1";
   });
   $('#sec').click(function(){
      flavorid = "2";
   })

thanks!

Hyung Suh