妄徒之命 2017-06-30 21:24 采纳率: 100%
浏览 72

jQuery Ajax排序Json

I have a little problem sorting JSON.

So here is my JSON data:

[
    {
        nom: "TERRES LATINES",
        numero: "0473343687",
        image: "http://s604712774.onlinehome.fr/bonapp/api/wp-content/uploads/2016/12/IMG_1165.jpg",
        timedate: "30/6/2017 - 7:5:45"
    },
    {
        nom: "TERRES LATINES",
        numero: "0473343687",
        image: "http://s604712774.onlinehome.fr/bonapp/api/wp-content/uploads/2016/12/IMG_1165.jpg",
        timedate: "30/6/2017 - 7:5:45"
    },
    {
        nom: "LE FURCO",
        numero: "0473156551",
        image: "http://s604712774.onlinehome.fr/bonapp/api/wp-content/uploads/2016/12/IMG_0843.jpg",
        timedate: "30/6/2017 - 18:4:6"
    },
    {
        nom: "A L’IDEE",
        numero: "0473193488",
        image: "http://s604712774.onlinehome.fr/bonapp/api/wp-content/uploads/2016/12/IMG_0299.jpg",
        timedate: "30/6/2017 - 23:5:56"
    }
]

With this Ajax jQuery code I can call all the entries in a table:

$.ajax({
    type: "GET",
    url: 'http://s604712774.onlinehome.fr/bonapp/app/text.json',
    dataType: 'json',
    success: function(data) {
        data.forEach(function(post) {
            $( '#contenttableau' ).append( '<tr><td>' + post.nom + '</td><td>' + post.numero + '</td><td>' + post.timedate + '</td><td></td></tr>' );
        });
    }
});

How can I get the number of time TERRES LATINES is present like in the following?

  • TERRES LATINES 2
  • LE FURCO 1
  • A L'IDEE 1
  • 写回答

2条回答 默认 最新

  • weixin_33724659 2017-06-30 21:38
    关注

    Create a map that holds the count for each key, and append it with the other values, something like

    $.ajax({
        type     : "GET",
        url      : 'http://s604712774.onlinehome.fr/bonapp/app/text.json',
        dataType : 'json',
        success  : function(data) {
    
            var map = {};
    
            data.forEach(function(post) {
                var key = post.numero;
    
                if ( key in map ) {
                    map[key]++;
                } else {
                    map[key] = 1;
                }
            });
    
            data.forEach(function(post) {
                var td1 = $('<td />', {html : post.nom}),
                    td2 = $('<td />', {html : post.numero}), 
                    td3 = $('<td />', {html : post.timedate}),
                    td4 = $('<td />', {html : map[post.numero]}),
                    tr  = $('<tr />');
    
                $( '#contenttableau' ).append( tr.append(td1, td2, td3, td4) );
            });
        }
    });
    

    FIDDLE

    评论

报告相同问题?

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作