2012-09-02 65 views
3

我无法使用jQuery和JSON过滤数据。我试图删除ID为“234”或发件人名称为“Alan Ford”的每个对象。因为我想制作一个将通过收件箱/发件箱分开的邮件系统,并且由于Alan Ford是发送人,所以我需要将他们分开,而不直接篡改JSON。使用JQUERY过滤JSON数据

下面是代码:

$(document).ready(function(){ 
    $.getJSON('public/js/data.json', function(json){ 
     $.each(json.data, function(i, data){ 
     if(data.from.id == "234"){   //if is not working correctly 
       $("p").remove(); 
       $("h1").remove(); 
       $("h2").remove(); 
       $("hr").remove()} 
      $("#inbox").append(
      '<div class="post">'+ 
      'To: '+data.to.name+''+ 
      ''+data.subject+''+ 
      ''+data.message_formatted+''+ 
      'added: '+data.date_sent_formatted.formatted+''+ 
      'Sender: '+data.from.name+''+ 
      '<hr />'+ 
     '</div>' 
    ); 
}); 
    }); 

}); 

而且JSON:

{ 
    "data":[ 
     { 
     "id":"2146", 
     "from":{ 
      "id":"234", 
      "name":"Alan Ford" 
     }, 
     "to":{ 
      "id":"4949", 
      "name":"Eric Owens" 
     }, 
     "type":"1", 
     "replyto":"0", 
     "date_sent":"1344359836", 
     "date_read":"0", 
     "subject":"test", 
     "message":"test inbox", 
     "message_formatted":"test inbox", 
     "date_sent_formatted":{ 
      "id":1196, 
      "timestamp":1344297600, 
      "month":8, 
      "day":7, 
      "year":2012, 
      "week":32, 
      "dayid":3, 
      "weekday":"Tue", 
      "mname":"Aug", 
      "formatted":"Aug 7, 2012" 
     }, 
     "date_read_formatted":[ 

     ] 
     }, 
     { 
     "id":"2048", 
     "from":{ 
      "id":"234", 
      "name":"Alan Ford" 
     }, 
     "to":{ 
      "id":"8110", 
      "name":"Event" 
     }, 
     "type":"1", 
     "replyto":"0", 
     "date_sent":"1343248577", 
     "date_read":"0", 
     "subject":"afd", 
     "message":"asdfads", 
     "message_formatted":"asdfads", 
     "date_sent_formatted":{ 
      "id":1184, 
      "timestamp":1343260800, 
      "month":7, 
      "day":26, 
      "year":2012, 
      "week":30, 
      "dayid":5, 
      "weekday":"Thu", 
      "mname":"Jul", 
      "formatted":"Jul 26, 2012" 
     }, 
     "date_read_formatted":[ 

     ] 
     }, 
     { 
     "id":"2047", 
     "from":{ 
      "id":"5245", 
      "name":"Ian Graham" 
     }, 
     "to":{ 
      "id":"234", 
      "name":"Alan Ford" 
     }, 
     "type":"1", 
     "replyto":"0", 
     "date_sent":"1343234342", 
     "date_read":"1343837983", 
     "subject":"re: This is SP", 
     "message":"Hi How are you[quote=Alan Ford]This is SP..Thais is SP[\/quote]", 
     "message_formatted":"Hi How are you", 
     "date_sent_formatted":{ 
      "id":1183, 
      "timestamp":1343174400, 
      "month":7, 
      "day":25, 
      "year":2012, 
      "week":30, 
      "dayid":4, 
      "weekday":"Wed", 
      "mname":"Jul", 
      "formatted":"Jul 25, 2012" 
     }, 
     "date_read_formatted":{ 
      "id":1190, 
      "timestamp":1343779200, 
      "month":8, 
      "day":1, 
      "year":2012, 
      "week":31, 
      "dayid":4, 
      "weekday":"Wed", 
      "mname":"Aug", 
      "formatted":"Aug 1, 2012" 
     } 
     }, 
     { 
     "id":"2046", 
     "from":{ 
      "id":"5245", 
      "name":"Ian Graham" 
     }, 
     "to":{ 
      "id":"234", 
      "name":"Alan Ford" 
     }, 
     "type":"1", 
     "replyto":"0", 
     "date_sent":"1343232908", 
     "date_read":"1344001216", 
     "subject":"Hello", 
     "message":"Hi", 
     "message_formatted":"Hi", 
     "date_sent_formatted":{ 
      "id":1183, 
      "timestamp":1343174400, 
      "month":7, 
      "day":25, 
      "year":2012, 
      "week":30, 
      "dayid":4, 
      "weekday":"Wed", 
      "mname":"Jul", 
      "formatted":"Jul 25, 2012" 
     }, 
     "date_read_formatted":{ 
      "id":1192, 
      "timestamp":1343952000, 
      "month":8, 
      "day":3, 
      "year":2012, 
      "week":31, 
      "dayid":6, 
      "weekday":"Fri", 
      "mname":"Aug", 
      "formatted":"Aug 3, 2012" 
     } 
     }, 
     { 
     "id":"2043", 
     "from":{ 
      "id":"234", 
      "name":"Alan Ford" 
     }, 
     "to":{ 
      "id":"5399", 
      "name":"Kob Bryant" 
     }, 
     "type":"1", 
     "replyto":"0", 
     "date_sent":"1342560365", 
     "date_read":"0", 
     "subject":"asda", 
     "message":"ASDASDAS", 
     "message_formatted":"ASDASDAS", 
     "date_sent_formatted":{ 
      "id":1176, 
      "timestamp":1342569600, 
      "month":7, 
      "day":18, 
      "year":2012, 
      "week":29, 
      "dayid":4, 
      "weekday":"Wed", 
      "mname":"Jul", 
      "formatted":"Jul 18, 2012" 
     }, 
     "date_read_formatted":[ 

     ] 
     }, 
     { 
     "id":"2042", 
     "from":{ 
      "id":"234", 
      "name":"Alan Ford" 
     }, 
     "to":{ 
      "id":"5226", 
      "name":"1341478142_Kareena kapoor" 
     }, 
     "type":"1", 
     "replyto":"0", 
     "date_sent":"1341364262", 
     "date_read":"0", 
     "subject":"adaasd", 
     "message":"asdadadas", 
     "message_formatted":"asdadadas", 
     "date_sent_formatted":{ 
      "id":1162, 
      "timestamp":1341360000, 
      "month":7, 
      "day":4, 
      "year":2012, 
      "week":27, 
      "dayid":4, 
      "weekday":"Wed", 
      "mname":"Jul", 
      "formatted":"Jul 4, 2012" 
     }, 
     "date_read_formatted":[ 

     ] 
     }, 
     { 
     "id":"2041", 
     "from":{ 
      "id":"234", 
      "name":"Alan Ford" 
     }, 
     "to":{ 
      "id":"5226", 
      "name":"1341478142_Kareena kapoor" 
     }, 
     "type":"1", 
     "replyto":"0", 
     "date_sent":"1341091202", 
     "date_read":"0", 
     "subject":"asdasd", 
     "message":"asdasfagfsdgsgsgasg", 
     "message_formatted":"asdasfagfsdgsgsgasg", 
     "date_sent_formatted":{ 
      "id":1159, 
      "timestamp":1341100800, 
      "month":7, 
      "day":1, 
      "year":2012, 
      "week":26, 
      "dayid":1, 
      "weekday":"Sun", 
      "mname":"Jul", 
      "formatted":"Jul 1, 2012" 
     }, 
     "date_read_formatted":[ 

     ] 
     }, 
     { 
     "id":"2040", 
     "from":{ 
      "id":"234", 
      "name":"Alan Ford" 
     }, 
     "to":{ 
      "id":"5226", 
      "name":"1341478142_Kareena kapoor" 
     }, 
     "type":"1", 
     "replyto":"0", 
     "date_sent":"1341091171", 
     "date_read":"0", 
     "subject":"asdad", 
     "message":"adasdasdada", 
     "message_formatted":"adasdasdada", 
     "date_sent_formatted":{ 
      "id":1159, 
      "timestamp":1341100800, 
      "month":7, 
      "day":1, 
      "year":2012, 
      "week":26, 
      "dayid":1, 
      "weekday":"Sun", 
      "mname":"Jul", 
      "formatted":"Jul 1, 2012" 
     }, 
     "date_read_formatted":[ 

     ] 
     }, 
     { 
     "id":"2039", 
     "from":{ 
      "id":"234", 
      "name":"Alan Ford" 
     }, 
     "to":{ 
      "id":"5226", 
      "name":"1341478142_Kareena kapoor" 
     }, 
     "type":"1", 
     "replyto":"0", 
     "date_sent":"1341091049", 
     "date_read":"0", 
     "subject":"Events", 
     "message":"Good to hear that.", 
     "message_formatted":"Good to hear that.", 
     "date_sent_formatted":{ 
      "id":1159, 
      "timestamp":1341100800, 
      "month":7, 
      "day":1, 
      "year":2012, 
      "week":26, 
      "dayid":1, 
      "weekday":"Sun", 
      "mname":"Jul", 
      "formatted":"Jul 1, 2012" 
     }, 
     "date_read_formatted":[ 

     ] 
     }, 
     { 
     "id":"2038", 
     "from":{ 
      "id":"234", 
      "name":"Alan Ford" 
     }, 
     "to":{ 
      "id":"4982", 
      "name":"Hamza Nadeem" 
     }, 
     "type":"1", 
     "replyto":"0", 
     "date_sent":"1341053992", 
     "date_read":"0", 
     "subject":"adfads", 
     "message":"agsgsggasgasgasas", 
     "message_formatted":"agsgsggasgasgasas", 
     "date_sent_formatted":{ 
      "id":1158, 
      "timestamp":1341014400, 
      "month":6, 
      "day":30, 
      "year":2012, 
      "week":26, 
      "dayid":7, 
      "weekday":"Sat", 
      "mname":"Jun", 
      "formatted":"Jun 30, 2012" 
     }, 
     "date_read_formatted":[ 

     ] 
     }, 
     { 
     "id":"2037", 
     "from":{ 
      "id":"234", 
      "name":"Alan Ford" 
     }, 
     "to":{ 
      "id":"4949", 
      "name":"Eric Owens" 
     }, 
     "type":"1", 
     "replyto":"0", 
     "date_sent":"1340985832", 
     "date_read":"0", 
     "subject":"test", 
     "message":"testtest", 
     "message_formatted":"testtest", 
     "date_sent_formatted":{ 
      "id":1157, 
      "timestamp":1340928000, 
      "month":6, 
      "day":29, 
      "year":2012, 
      "week":26, 
      "dayid":6, 
      "weekday":"Fri", 
      "mname":"Jun", 
      "formatted":"Jun 29, 2012" 
     }, 
     "date_read_formatted":[ 

     ] 
     }, 
     { 
     "id":"2036", 
     "from":{ 
      "id":"234", 
      "name":"Alan Ford" 
     }, 
     "to":{ 
      "id":"5730", 
      "name":"Mobile Employee" 
     }, 
     "type":"1", 
     "replyto":"0", 
     "date_sent":"1340878624", 
     "date_read":"0", 
     "subject":"This is SP", 
     "message":"This is SP..Thais is SP", 
     "message_formatted":"This is SP..Thais is SP", 
     "date_sent_formatted":{ 
      "id":1156, 
      "timestamp":1340841600, 
      "month":6, 
      "day":28, 
      "year":2012, 
      "week":26, 
      "dayid":5, 
      "weekday":"Thu", 
      "mname":"Jun", 
      "formatted":"Jun 28, 2012" 
     }, 
     "date_read_formatted":[ 

     ] 
     }, 
     { 
     "id":"2035", 
     "from":{ 
      "id":"234", 
      "name":"Alan Ford" 
     }, 
     "to":{ 
      "id":"5735", 
      "name":"Fsdfsdghh" 
     }, 
     "type":"1", 
     "replyto":"0", 
     "date_sent":"1340878624", 
     "date_read":"0", 
     "subject":"This is SP", 
     "message":"This is SP..Thais is SP", 
     "message_formatted":"This is SP..Thais is SP", 
     "date_sent_formatted":{ 
      "id":1156, 
      "timestamp":1340841600, 
      "month":6, 
      "day":28, 
      "year":2012, 
      "week":26, 
      "dayid":5, 
      "weekday":"Thu", 
      "mname":"Jun", 
      "formatted":"Jun 28, 2012" 
     }, 
     "date_read_formatted":[ 

     ] 
     }, 
     { 
     "id":"2034", 
     "from":{ 
      "id":"234", 
      "name":"Alan Ford" 
     }, 
     "to":{ 
      "id":"5743", 
      "name":"double dots3" 
     }, 
     "type":"1", 
     "replyto":"0", 
     "date_sent":"1340878624", 
     "date_read":"0", 
     "subject":"This is SP", 
     "message":"This is SP..Thais is SP", 
     "message_formatted":"This is SP..Thais is SP", 
     "date_sent_formatted":{ 
      "id":1156, 
      "timestamp":1340841600, 
      "month":6, 
      "day":28, 
      "year":2012, 
      "week":26, 
      "dayid":5, 
      "weekday":"Thu", 
      "mname":"Jun", 
      "formatted":"Jun 28, 2012" 
     }, 
     "date_read_formatted":[ 

     ] 
     }, 
     { 
     "id":"2033", 
     "from":{ 
      "id":"234", 
      "name":"Alan Ford" 
     }, 
     "to":{ 
      "id":"5742", 
      "name":"double dots2" 
     }, 
     "type":"1", 
     "replyto":"0", 
     "date_sent":"1340878624", 
     "date_read":"0", 
     "subject":"This is SP", 
     "message":"This is SP..Thais is SP", 
     "message_formatted":"This is SP..Thais is SP", 
     "date_sent_formatted":{ 
      "id":1156, 
      "timestamp":1340841600, 
      "month":6, 
      "day":28, 
      "year":2012, 
      "week":26, 
      "dayid":5, 
      "weekday":"Thu", 
      "mname":"Jun", 
      "formatted":"Jun 28, 2012" 
     }, 
     "date_read_formatted":[ 

     ] 
     }, 
     { 
     "id":"2032", 
     "from":{ 
      "id":"234", 
      "name":"Alan Ford" 
     }, 
     "to":{ 
      "id":"5734", 
      "name":"Adalph" 
     }, 
     "type":"1", 
     "replyto":"0", 
     "date_sent":"1340878624", 
     "date_read":"0", 
     "subject":"This is SP", 
     "message":"This is SP..Thais is SP", 
     "message_formatted":"This is SP..Thais is SP", 
     "date_sent_formatted":{ 
      "id":1156, 
      "timestamp":1340841600, 
      "month":6, 
      "day":28, 
      "year":2012, 
      "week":26, 
      "dayid":5, 
      "weekday":"Thu", 
      "mname":"Jun", 
      "formatted":"Jun 28, 2012" 
     }, 
     "date_read_formatted":[ 

     ] 
     }, 
     { 
     "id":"2031", 
     "from":{ 
      "id":"234", 
      "name":"Alan Ford" 
     }, 
     "to":{ 
      "id":"5731", 
      "name":"Mobile Scheduler" 
     }, 
     "type":"1", 
     "replyto":"0", 
     "date_sent":"1340878624", 
     "date_read":"0", 
     "subject":"This is SP", 
     "message":"This is SP..Thais is SP", 
     "message_formatted":"This is SP..Thais is SP", 
     "date_sent_formatted":{ 
      "id":1156, 
      "timestamp":1340841600, 
      "month":6, 
      "day":28, 
      "year":2012, 
      "week":26, 
      "dayid":5, 
      "weekday":"Thu", 
      "mname":"Jun", 
      "formatted":"Jun 28, 2012" 
     }, 
     "date_read_formatted":[ 

     ] 
     }, 
     { 
     "id":"2030", 
     "from":{ 
      "id":"234", 
      "name":"Alan Ford" 
     }, 
     "to":{ 
      "id":"4949", 
      "name":"Eric Owens" 
     }, 
     "type":"1", 
     "replyto":"0", 
     "date_sent":"1340878624", 
     "date_read":"0", 
     "subject":"This is SP", 
     "message":"This is SP..Thais is SP", 
     "message_formatted":"This is SP..Thais is SP", 
     "date_sent_formatted":{ 
      "id":1156, 
      "timestamp":1340841600, 
      "month":6, 
      "day":28, 
      "year":2012, 
      "week":26, 
      "dayid":5, 
      "weekday":"Thu", 
      "mname":"Jun", 
      "formatted":"Jun 28, 2012" 
     }, 
     "date_read_formatted":[ 

     ] 
     }, 
     { 
     "id":"2029", 
     "from":{ 
      "id":"234", 
      "name":"Alan Ford" 
     }, 
     "to":{ 
      "id":"5628", 
      "name":"Test email" 
     }, 
     "type":"1", 
     "replyto":"0", 
     "date_sent":"1340878624", 
     "date_read":"0", 
     "subject":"This is SP", 
     "message":"This is SP..Thais is SP", 
     "message_formatted":"This is SP..Thais is SP", 
     "date_sent_formatted":{ 
      "id":1156, 
      "timestamp":1340841600, 
      "month":6, 
      "day":28, 
      "year":2012, 
      "week":26, 
      "dayid":5, 
      "weekday":"Thu", 
      "mname":"Jun", 
      "formatted":"Jun 28, 2012" 
     }, 
     "date_read_formatted":[ 

     ] 
     }, 
     { 
     "id":"2028", 
     "from":{ 
      "id":"234", 
      "name":"Alan Ford" 
     }, 
     "to":{ 
      "id":"5627", 
      "name":"1341059685_dejan email test" 
     }, 
     "type":"1", 
     "replyto":"0", 
     "date_sent":"1340878624", 
     "date_read":"0", 
     "subject":"This is SP", 
     "message":"This is SP..Thais is SP", 
     "message_formatted":"This is SP..Thais is SP", 
     "date_sent_formatted":{ 
      "id":1156, 
      "timestamp":1340841600, 
      "month":6, 
      "day":28, 
      "year":2012, 
      "week":26, 
      "dayid":5, 
      "weekday":"Thu", 
      "mname":"Jun", 
      "formatted":"Jun 28, 2012" 
     }, 
     "date_read_formatted":[ 

     ] 
     }, 
     { 
     "id":"2027", 
     "from":{ 
      "id":"234", 
      "name":"Alan Ford" 
     }, 
     "to":{ 
      "id":"5625", 
      "name":"Test" 
     }, 
     "type":"1", 
     "replyto":"0", 
     "date_sent":"1340878624", 
     "date_read":"0", 
     "subject":"This is SP", 
     "message":"This is SP..Thais is SP", 
     "message_formatted":"This is SP..Thais is SP", 
     "date_sent_formatted":{ 
      "id":1156, 
      "timestamp":1340841600, 
      "month":6, 
      "day":28, 
      "year":2012, 
      "week":26, 
      "dayid":5, 
      "weekday":"Thu", 
      "mname":"Jun", 
      "formatted":"Jun 28, 2012" 
     }, 
     "date_read_formatted":[ 

     ] 
     }, 
     { 
     "id":"2026", 
     "from":{ 
      "id":"234", 
      "name":"Alan Ford" 
     }, 
     "to":{ 
      "id":"5623", 
      "name":"El Homo" 
     }, 
     "type":"1", 
     "replyto":"0", 
     "date_sent":"1340878624", 
     "date_read":"0", 
     "subject":"This is SP", 
     "message":"This is SP..Thais is SP", 
     "message_formatted":"This is SP..Thais is SP", 
     "date_sent_formatted":{ 
      "id":1156, 
      "timestamp":1340841600, 
      "month":6, 
      "day":28, 
      "year":2012, 
      "week":26, 
      "dayid":5, 
      "weekday":"Thu", 
      "mname":"Jun", 
      "formatted":"Jun 28, 2012" 
     }, 
     "date_read_formatted":[ 

     ] 
     }, 
     { 
     "id":"2025", 
     "from":{ 
      "id":"234", 
      "name":"Alan Ford" 
     }, 
     "to":{ 
      "id":"5618", 
      "name":"Tiengo Mass" 
     }, 
     "type":"1", 
     "replyto":"0", 
     "date_sent":"1340878624", 
     "date_read":"0", 
     "subject":"This is SP", 
     "message":"This is SP..Thais is SP", 
     "message_formatted":"This is SP..Thais is SP", 
     "date_sent_formatted":{ 
      "id":1156, 
      "timestamp":1340841600, 
      "month":6, 
      "day":28, 
      "year":2012, 
      "week":26, 
      "dayid":5, 
      "weekday":"Thu", 
      "mname":"Jun", 
      "formatted":"Jun 28, 2012" 
     }, 
     "date_read_formatted":[ 

     ] 
     }, 
     { 
     "id":"2024", 
     "from":{ 
      "id":"234", 
      "name":"Alan Ford" 
     }, 
     "to":{ 
      "id":"5608", 
      "name":"1344517115_Test22" 
     }, 
     "type":"1", 
     "replyto":"0", 
     "date_sent":"1340878624", 
     "date_read":"0", 
     "subject":"This is SP", 
     "message":"This is SP..Thais is SP", 
     "message_formatted":"This is SP..Thais is SP", 
     "date_sent_formatted":{ 
      "id":1156, 
      "timestamp":1340841600, 
      "month":6, 
      "day":28, 
      "year":2012, 
      "week":26, 
      "dayid":5, 
      "weekday":"Thu", 
      "mname":"Jun", 
      "formatted":"Jun 28, 2012" 
     }, 
     "date_read_formatted":[ 

     ] 
     }, 
     { 
     "id":"2023", 
     "from":{ 
      "id":"234", 
      "name":"Alan Ford" 
     }, 
     "to":{ 
      "id":"5606", 
      "name":"1341474388_sgdfgdfg" 
     }, 
     "type":"1", 
     "replyto":"0", 
     "date_sent":"1340878624", 
     "date_read":"0", 
     "subject":"This is SP", 
     "message":"This is SP..Thais is SP", 
     "message_formatted":"This is SP..Thais is SP", 
     "date_sent_formatted":{ 
      "id":1156, 
      "timestamp":1340841600, 
      "month":6, 
      "day":28, 
      "year":2012, 
      "week":26, 
      "dayid":5, 
      "weekday":"Thu", 
      "mname":"Jun", 
      "formatted":"Jun 28, 2012" 
     }, 
     "date_read_formatted":[ 

     ] 
     } 
    ] 
} 

回答

3

我不明白你为什么会想,如果你通过它需要循环,除去从JSON什么无论如何打印消息(至少这是你的示例引导我思考)。

所以我只是添加此条件,您的JS:

$.each(json.data, function(i, data){ 
    if(data.from.id != 234 && data.from.name != 'Alan Ford'){ 
     $("#inbox").append(
      '<div class="post">'+ 
      'To: '+data.to.name+''+ 
      ''+data.subject+''+ 
      ''+data.message_formatted+''+ 
      'added: '+data.date_sent_formatted.formatted+''+ 
      'Sender: '+data.from.name+''+ 
      '<hr />'+ 
      '</div>' 
     ); 
    } 
});​ 

此外,你可能想通过自己更换危险字符或使用.text()功能正确逃生你打印的每一个变量来自jQuery。


最后的忠告:编辑DOM需要时间,所以你最好在处理大量邮件的附加只有计划一次:

var output = ''; 
$.each(json.data, function(i, data){ 
    if(data.from.id != 234 && data.from.name != 'Alan Ford'){ 
     output += 
      '<div class="post">'+ 
      'To: '+data.to.name+''+ 
      ''+data.subject+''+ 
      ''+data.message_formatted+''+ 
      'added: '+data.date_sent_formatted.formatted+''+ 
      'Sender: '+data.from.name+''+ 
      '<hr />'+ 
      '</div>'; 
    } 
}); 
$("#inbox").append(output);​ 

事情是这样的:http://jsfiddle.net/pioul/Hy2nd/

+0

谢谢你这么多,为了这么彻底和有用的解释!我是一个血腥的傻瓜! 说实话,我从来没有使用JSON,也没有通过Jquery处理它。但仍然如此简单......:D 谢谢你,你帮了我很多! – Inexorable