Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/373.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 如何在jquery中从json中获取不同的值_Javascript_Jquery_Json - Fatal编程技术网

Javascript 如何在jquery中从json中获取不同的值

Javascript 如何在jquery中从json中获取不同的值,javascript,jquery,json,Javascript,Jquery,Json,我有一个jquery json请求,在该json数据中,我希望能够按唯一值排序。所以我有 { "people": [{ "pbid": "626", "birthDate": "1976-02-06", "name": 'name' }, { "pbid": "648", "birthDate": "1987-05-22", "name": 'name' }, .....

我有一个jquery json请求,在该json数据中,我希望能够按唯一值排序。所以我有

{
  "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);
  })
}
但是,我完全不知道如何高效地只获取唯一的生日,并按年份(或任何其他个人数据)进行排序

我正在努力做到这一点,并对此保持高效率(我希望这是可能的)


谢谢

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

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
    }
}
以下是我的看法:

function getUniqueBirthdays(data){
    var birthdays = [];
    $.each(data.people, function(){
        if ($.inArray(this.birthDate,birthdays) === -1) {
            birthdays.push(this.birthDate);
        }
    });
    return birthdays.sort();
}

我一直看到这个。与其使用函数(i,person)alert(person.birthDate)不如使用函数()alert(this.birthDate)哇,这太棒了,让我了解了其中的大部分(或很多),但我仍然坚持的是,一旦我在数组中有了unique,我如何使用它查询人名?我基本上是想说得到名字在哪里生日=d?谢谢。这是可行的,但我实际上希望有一种更好的方法来处理json,这样我以后就可以轻松地按另一个变量排序,而无需重新创建新的数组。哦,这确实有效。谢谢,这并不完全完美,但如果需要,可以将生日作为变量提供。我更新了我的样品
function getUniqueBirthdays(data){
    var birthdays = [];
    $.each(data.people, function(){
        if ($.inArray(this.birthDate,birthdays) === -1) {
            birthdays.push(this.birthDate);
        }
    });
    return birthdays.sort();
}