使用json Array中的键/值进行过滤

时间:2016-04-07 12:53:09

标签: javascript jquery

这是我的代码,我只想显示有“帮助”作为标记的项目,而不显示另一项。我真的不知道我该怎么做。

function displayall(newid){
  $.ajax({
      url: "https://cubber.zendesk.com/api/v2/users/"+newid+"/tickets/requested.json",
      type: 'GET',
      cors: true,
      dataType: 'json',
      contentType:'application/json',
      secure: true,
      beforeSend: function (xhr) {
          xhr.setRequestHeader ("Authorization", "Basic " + btoa(""));
      },
      success: function (data){
        var sortbydate = data.tickets.sort(function(a,b){ return new Date(b.created_at)- new Date(a.created_at); });
        for (i = 0; i < data.tickets.length; i++) {

            var myticket = data.tickets[i];
            var mydate = data.tickets[i].created_at;
            var created = moment(mydate).format("MM-DD-YY");
            var mytitle = data.tickets[i].subject;
            var description = data.tickets[i].description;
            var status = data.tickets[i].status;
            var ticketid = data.tickets[i].id;
            var tag = data.tickets[i].tags[0];





$("#mylist").append('<li class="row col-md-12 listing" id="newlist" value="'+ticketid+'" onclick="ticketcontent('+ticketid+","+newid+')">'+ '<span class="class_'+status+' otherClasses">' + status + '</span>'+'<div class="identifiant fixed col-md-2">'+" #"+ ticketid +'</div>'+'<div class="identifiant col-md-2">'+tag+'</div>'+'<div class="identifiant col-md-4">'+mytitle +'</div>'+'<div class="identifiant datefixed col-md-2">'+created+'</div>'+'</li>');
        }
      }
    })
}

如果我做console.log(data.ticket [i]),这就是我得到的:

enter image description here

2 个答案:

答案 0 :(得分:4)

您正在寻找的是:

var filteredTickets = data.tickets.filter(function(ticket) {
  return ticket.tags.indexOf('assistance') >= 0;
});

答案 1 :(得分:1)

尝试使用data.tickets.filter()

data.tickets = data.tickets.filter(function(ticket){
  return ticket.tags[0] === 'assistance';
});