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"
}]}
이것에서 나는 가중치 (전체 점수)가 높은 것과 같은 순서를 보여주고 싶습니다. 그것은 마지막에 상위 하위에 올 것입니다.
설명으로 필터링하려면 어떻게해야합니까? 이것은 내 코드입니다.
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*/
이것은 간단합니다. 응답 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);
도움이 되었기를 바랍니다 :)
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다