我想要做的是比較數組中的項目以確定首先列出誰,爲了簡單起見,在我看來,所有人員數據都存儲在同一個地方。我不知道將這些數據分開會更容易還是更高效。JavaScript/jQuery比較數組中的項目
這裏是陣列,縮小爲了便於閱讀。
var People = [{
"Name": "Person1",
"SN": "1",
"First": "5",
"Second": "6",
"Third": "11",
"Fourth": "7",
"Fifth": "8",
"Sixth": "1",
"Seventh": "10",
"Eigth": "4",
"Ninth": "3",
"Tenth": "2",
"Eleventh": "13",
"Twelth": "9",
"Thirteenth": "12",
"RDO1": 2,
"RDO2": 3
}, {
"Name": "Person2",
"SN": "2",
"First": "6",
"Second": "5",
"Third": "10",
"Fourth": "9",
"Fifth": "7",
"Sixth": "8",
"Seventh": "1",
"Eigth": "4",
"Ninth": "3",
"Tenth": "2",
"Eleventh": "13",
"Twelth": "11",
"Thirteenth": "12",
"RDO1": 2,
"RDO2": 3
}, {
"Name": "Person3",
"SN": "3",
"First": "6",
"Second": "9",
"Third": "7",
"Fourth": "10",
"Fifth": "8",
"Sixth": "5",
"Seventh": "4",
"Eigth": "2",
"Ninth": "3",
"Tenth": "13",
"Eleventh": "11",
"Twelth": "12",
"Thirteenth": "1",
"RDO1": 4,
"RDO2": 5
}];
我試過了一堆嵌套的if,for循環,以及循環回彼此沒有成功的函數。
所以我試圖完成的是它將抓住「第一」的選擇,並與數組中的其他人比較,如果其他人有相同的「第一」的選擇,它比較SN號,看看哪一個較低。
因此,在上述數組中,當代碼來到Person3並將「6」看作第一選擇時,它會查看是否有其他人有6個作爲他們的第一選擇,並且通知Person2也將其選中。然後比較SN號碼,並且Person3沒有給出6,因爲Person2具有較低的號碼,因此它繼續到Person3的Second並且再次進行相同的檢查。
我一直在砸我的頭,一小會兒現在,如果任何人都可以擺脫任何光線,我將非常感激。
編輯 - 更多的代碼
function getPref(index1) {
var sNumb = dsobj[index1].SN;
for (var i = 0; i < dsobj.length; i++) {
if (i != index1) { // Ensure you aren't checking the same person
if (sNumb > dsobj[i].SN) { //Check to see if the SN is lower than the person you are checking
if (dsobj[index1].First === dsobj[i].First) {
if (dsobj[index1].Second === dsobj[i].Second) {
if (dsobj[index1].Third === dsobj[i].Third) {
if (dsobj[index1].Fourth === dsobj[i].Fourth) {
if (dsobj[index1].Fifth === dsobj[i].Fifth) {
if (dsobj[index1].Sixth === dsobj[i].Sixth) {
if (dsobj[index1].Seventh === dsobj[i].Seventh) {
if (dsobj[index1].Eigth === dsobj[i].Eigth) {
if (dsobj[index1].Ninth === dsobj[i].Ninth) {
if (dsobj[index1].Tenth === dsobj[i].Tenth) {
if (dsobj[index1].Eleventh === dsobj[i].Eleventh) {
if (dsobj[index1].Twelth === dsobj[i].Twelth) {
if (dsobj[index1].Thirteenth === dsobj[i].Thirteenth) {
} else {
endChoice = dsobj[index1].Thirteenth;
}
} else {
endChoice = dsobj[index1].Twelth;
}
} else {
endChoice = dsobj[index1].Eleventh;
}
} else {
endChoice = dsobj[index1].Tenth;
}
} else {
endChoice = dsobj[index1].Ninth;
}
} else {
endChoice = dsobj[index1].Eigth;
}
} else {
endChoice = dsobj[index1].Seventh;
}
} else {
endChoice = dsobj[index1].Sixth;
}
} else {
endChoice = dsobj[index1].Fifth;
}
} else {
endChoice = dsobj[index1].Fourth;
}
} else {
endChoice = dsobj[index1].Third;
}
} else {
endChoice = dsobj[index1].Second;
}
} else {
endChoice = dsobj[index1].First;
}
} else {
endChoice = dsobj[index1].First;
}
} else {
continue;
}
};
return endChoice
}
function displayPref(index,id,wd) {
endChoice = getPref(index);
if (wd === dsobj[index].RDO1 || wd === dsobj[index].RDO2) {
$('#'+id+'').append("<td>None</td>");
} else {
$('#'+id+'').append("<td>"+endChoice+"</td>");
}
}
@ Hintshigen將所有的規則是什麼願望後輸出? – ozil
你看過實用程序[jQuery.inArray()](https://api.jquery.com/jQuery.inArray/)嗎? – urbz
@ ozil期望的輸出將是人名,然後是選擇旁邊的選項,但只有1人可以有1個選擇,所以兩個人不能有選擇6顯示 – Hintshigen