Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript &引用;“过滤器”;通过ajax的json(jquery)_Javascript_Jquery_Json_Ajax - Fatal编程技术网

Javascript &引用;“过滤器”;通过ajax的json(jquery)

Javascript &引用;“过滤器”;通过ajax的json(jquery),javascript,jquery,json,ajax,Javascript,Jquery,Json,Ajax,我试图通过ajax过滤json数组,但不知道如何过滤 { posts: [{ "image": "images/bbtv.jpg", "group": "a" }, { "image": "images/grow.jpg", "group": "b" }, { "image": "images/tabs.jpg", "group": "a" }, { "image": "images/bia.jpg", "group

我试图通过ajax过滤json数组,但不知道如何过滤

{
  posts: [{
    "image": "images/bbtv.jpg",
    "group": "a"
  }, {
    "image": "images/grow.jpg",
    "group": "b"
  }, {
    "image": "images/tabs.jpg",
    "group": "a"
  }, {
    "image": "images/bia.jpg",
    "group": "b"
  }]
}
我想要它,以便我只能显示A组或B组中的项目

我必须如何更改ajax以过滤内容

$.ajax({
 type: "GET",
 url: "category/all.js",
 dataType: "json",
 cache: false,
 contentType: "application/json",
 success: function(data) {
$('#folio').html("<ul/>");
$.each(data.posts, function(i,post){
   $('#folio ul').append('<li><div class="boxgrid captionfull"><img src="' + post.image + '" /></div></li>');
});

initBinding();
 },
 error: function(xhr, status, error) {
   alert(xhr.status);
 }
$.ajax({
键入:“获取”,
url:“category/all.js”,
数据类型:“json”,
cache:false,
contentType:“应用程序/json”,
成功:功能(数据){
$('对开本').html(“
    ”); $.each(data.posts,function(i,post){ $('folio ul')。追加('li>'); }); initBinding(); }, 错误:函数(xhr、状态、错误){ 警报(xhr.状态); }
}))

此外,如何使每个链接处理过滤器

<a href="category/all.js">Group A</a> <a href="category/all.js">Group B</a>

对于所有这些问题,我很抱歉,我似乎无法找到解决方案。。 任何方向正确的帮助都将不胜感激


谢谢

您很可能需要编写一个过滤函数:

function filterGroup(obj, filteredGroup) {
  var resultObj = $.extend({},obj);

  for (var i in obj) {
    if ( obj.hasOwnProperty(i) ) {
      if ( obj[i].group && obj[i].group !== filteredGroup ) {
        delete resultObj[i];
      }
    }
  }
  return resultObj;
}
然后,您只需通过该过滤器运行数据。您可能还想切换到包含一堆JSON的帖子,如下所示

$.ajax({
  type: "POST",
  url: "category/all.js",
  dataType: "json",
  cache: false,
  data: {"posts": filterGroup(posts, 'a')},
  contentType: "application/json",
  success: function(data) {
    $('#folio').html("<ul/>");
    $.each(data.posts, function(i,post){
      $('#folio ul').append('<li><div class="boxgrid captionfull"><img src="' + 
                           post.image + '" /></div></li>');
    });
  }
});

您很可能需要编写一个过滤函数:

function filterGroup(obj, filteredGroup) {
  var resultObj = $.extend({},obj);

  for (var i in obj) {
    if ( obj.hasOwnProperty(i) ) {
      if ( obj[i].group && obj[i].group !== filteredGroup ) {
        delete resultObj[i];
      }
    }
  }
  return resultObj;
}
然后,您只需通过该过滤器运行数据。您可能还想切换到包含一堆JSON的帖子,如下所示

$.ajax({
  type: "POST",
  url: "category/all.js",
  dataType: "json",
  cache: false,
  data: {"posts": filterGroup(posts, 'a')},
  contentType: "application/json",
  success: function(data) {
    $('#folio').html("<ul/>");
    $.each(data.posts, function(i,post){
      $('#folio ul').append('<li><div class="boxgrid captionfull"><img src="' + 
                           post.image + '" /></div></li>');
    });
  }
});

你的代码从原始对象中删除属性,我想你应该在删除循环中使用
resultObj
,因为过滤
'a'
'b'
并返回将导致当前代码为空的
someDataObject
。谢谢Nick,修复了它。TWA只是一个疏忽。您的代码从原始对象中删除了属性,我认为您应该在删除循环中使用
resultObj
,因为过滤
'a'
'b'
,然后返回将导致当前代码为空的
someDataObject
。谢谢Nick,修复了它。这只是一个疏忽。