【问题标题】:How to filter by Desc in Javascript after ajax responseajax响应后如何在Javascript中按Desc过滤
【发布时间】:2017-08-03 02:45:46
【问题描述】:

Ajax 响应我得到这样的结果:

"students":[{
"student_id":"27",
"job_id":"12",
"128":"8",
"63":"9",
"3":"10",
"weightage":"5.6000"}
,{"student_id":"22",
"job_id":"12",
"128":"5",
"63":"6",
"3":"8",
"weightage":"9.1000"
}]}

从这里我想显示相同的顺序,因为权重(总分)很高,它应该排在最后。

如何按 Desc 过滤? 这是我的代码:

console.log(response['students']);
$.each(response['students'], function(k, student) {                 
    $("#stu"+student.student_id).removeClass('btn-danger reject-student');                  
    $("#stu"+student.student_id).addClass('btn-success select-student');                    
    $("#stu"+student.student_id).text('Student Selected');                  
});             
/*Sorting select-student students at one place starts here*/                
var rows = document.querySelectorAll('table tbody tr.sorted');              
for (i = 0; i < rows.length; i++) {                 
    if (rows[i].querySelector('span.reject-student')) {                     
        rows[i].closest('tbody').appendChild(rows[i]);                      
    }                   
}               
/*Sorting select-student students at one place ends here*/              

【问题讨论】:

  • 当您说“过滤器”时,您的意思是“排序”吗?而“desc”的意思是“下降”? (您的问题标题“按 Desc 过滤”听起来您想根据描述字段过滤掉数据......)
  • 对不起,我的意思是排序
  • 为什么不在服务器端使用 asort 对数组进行排序,然后以 json 格式发送响应。

标签: javascript php ajax filter


【解决方案1】:
var rows = document.querySelectorAll('table tbody tr.sorted');      
$.each(response['students'], function(k, student) {     
    $("#stu"+student.student_id).removeClass('btn-danger reject-student');                  
    $("#stu"+student.student_id).addClass('btn-success select-student');                    
    $("#stu"+student.student_id).text('Student Selected');  
    /*  Sorting by student overall score starts here   */
    for (i = 0; i < rows.length; i++) {                 
        if (rows[i].querySelector('span#stu'+student.student_id)) {                     
            rows[i].closest('tbody').appendChild(rows[i]);                      
        }                   
    }
    /*  Sorting by student overall score ends here   */
});             

【讨论】:

    【解决方案2】:

    这很简单。将您的响应 JSON 字符串解析为一个对象。请尝试以下方法:

    // Array of student objects
    var students = JSON.parse("Your response json string");
    
    //Sort this array by weightage property in descending order
    students.sort(function(a,b){
       return b.weightage - a.weightage; // descending order
    });
    

    请看下面的小提琴:

    var response = '[{"student_id": "27","job_id": "12","128": "8","63": "9","3": "10","weightage": "5.6000"}, {"student_id": "22","job_id": "12","128": "5","63": "6","3": "8","weightage": "9.1000"}]';
    
    var students = JSON.parse(response);
    
    students.sort(function(a,b){
       return b.weightage - a.weightage; // descending order
    });
    
    alert(students[0].weightage);
    alert(students[1].weightage);

    希望这会有所帮助:)

    【讨论】:

    • 这是有效的。我已经更新了我的答案。请参阅小提琴 :) 请将其标记为答案,如果对您有帮助,请点赞。谢谢:)
    • SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
    • 您的 Json 字符串无效。请在给定的小提琴中查看有效的 json 格式。谢谢:)
    • 这是有效的 json 字符串。请将您的与此进行比较: var response = '[{"student_id": "27","job_id": "12","128": "8","63": "9","3": "10 ","weightage": "5.6000"}, {"student_id": "22","job_id": "12","128": "5","63": "6","3": "8 ","weightage": "9.1000"}]';
    • @AbdulWaheed - 是的。我提供的解决方案按权重降序对数组进行排序。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-31
    • 2011-11-26
    • 2021-10-28
    • 2019-09-08
    • 1970-01-01
    相关资源
    最近更新 更多