开发者

how to filter and then load from json file?

the question might be a bit confus开发者_StackOverflowing :) i have this json:

[
{"media_path":"2.jpg","type":"1","complete":"1"}, 
{"media_path":"2.jpg","type":"1","complete":"2"},
{"media_path":"2.jpg","type":"1","complete":"3"}
]

what i want to do is to load the "media_path" of the "complete":"2"

so far i have:

$.getJSON('image.json', function(data) {
$('<img/>').attr("src", item.media_path);
);

but somehow i need to filter "complete":"2"

thanks


Just iterate over the returned json and if item.complete === "2" load the image.

$.getJSON('image.json', function(data) {
  $.each(data, function(i, item){
    if(item.complete === "2"){
      $('<img/>').attr("src", item.media_path);
    }
  });
);

Simple example on jsfiddle.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜