如何从jquery中的json获取不同的值 [英] how to get distinct values from json in jquery

查看:78
本文介绍了如何从jquery中的json获取不同的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个jQuery JSON请求,在该JSON数据中,我希望能够按唯一值进行排序.所以我有

I've got a jquery json request and in that json data I want to be able to sort by unique values. so I have


{"people":[{"pbid":"626","birthDate":"1976-02-06","name":'name'},{"pbid":"648","birthDate":"1987-05-22","name":'name'},.....

到目前为止,我有这个


 function(data){
          $.each(data.people, function(i, person){
               alert(person.birthDate);

}

但是,我对于如何唯一有效地获取唯一的birthDate并按年份(或按任何其他个人数据进行的排序)的效率完全不知所措.

but, I am at a total loss as to how efficiently get only the unique birthDates, and sort them by year (or any sort by any other personal data).

我正在尝试做到这一点,并做到高效(我希望这是可能的).

I'm trying to do this, and be efficient about it (i'm hoping that is possible).

谢谢

推荐答案

我不确定这样做的性能如何,但是基本上我正在使用对象作为键/值字典.我还没有测试过,但是应该在循环中进行排序.

I'm not sure how performant this will be, but basically I'm using an object as a key/value dictionary. I haven't tested this, but this should be sorted in the loop.

function(data) {
    var birthDates = {};
    var param = "birthDate"
    $.each(data.people, function() {
        if (!birthDates[this[param]])
            birthDates[this[param]] = [];   
        birthDates[this[param]].push(this);
    });

    for(var d in birthDates) {
        // add d to array here
        // or do something with d
        // birthDates[d] is the array of people
    }
}

这篇关于如何从jquery中的json获取不同的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆