我正在制作一個匹配系統,其中重量小于或大于等于 15 的玩家將被匹配。
For example Player 105 has 2185 and player 132 has 2000. They'll be matched.
這是我匹配時控制臺日志中的示例資料。
2185_2200: Array(2)
0: {entryID: '105', eventID: '22', …}
1: {entryID: '132', eventID: '22', …}
length: 2
現在,我的問題是我也有一個沒有比賽的球員,他們是這樣顯示的:

有沒有一種方法可以讓我只使用 ARRAY(2) 控制臺.記錄那些資料?并忽略那些沒有比賽的球員?

當我不匹配的玩家只有 時9,我的 foreach 不會出錯,但是當它到達 時10,我得到一個 val.Foreach 不是一個函式。我想我的腳本中缺少另一個條件。我希望你能幫助我,我被困在這里好幾天了。預先感謝您并感謝您
html:
//I am appending my result here
<div id="resulta">
</div>
腳本:
function newCombine(data, difference) {
let nonMatched = [...data]
const groups = {}
for (let i = 0; i < nonMatched.length - 1; i ) {
const first = nonMatched[i]
inner: for (let j = nonMatched.length - 1; j > i; j--) {
const second = nonMatched[j]
const delta = Math.abs(first.weight - second.weight)
if (delta <= difference && first.entryName !== second.entryName) {
const groupKey = `${first.weight}_${second.weight}`
groups[groupKey] = [first, second]
nonMatched = nonMatched.filter(
obj => obj.entryID != first.entryID && obj.entryID != second.entryID
)
i = -1
break inner
}
}
}
return { ...groups, ...nonMatched }
}
$(document).ready(function() {
let resulta = [];
var html = "";
var entry_list =$('#entry_list1').DataTable({
"ajax": {
"url": "<?php echo site_url('report/controlget')?>",
"type": "get",
success: function(data) {
const source = data;
const result = newCombine(source, 15);
console.log(result);
var aaa = Object.entries(result)
var a = Object.keys(aaa);
a = a.map(o => {
return o.replace(/(_)/mg, '').replace(/(_)/mg, '');
})
var int_number = 9;
var int_length = ('' int_number).length;
console.log(int_length);
var count = 0;
for (var i = 0; i < a.length; i ) {
aaa[a[i]].forEach(function(val ,index) {
var asd = val.length;
console.log(Array.val);
if(asd == 2){
val.forEach(function(value, index) { // // when the keys reached 2 digits (10 - 11 -12 or so on), my console will error with a message "val.forEach is not a function".
var idaa = value.eventID ;
var entryIDs = index == 0 ? "entryIDM[]" : "entryIDW[]"
var players = index == 0 ? "playerM[]" : "playerW[]"
var weights = index == 0 ? "weightM[]" : "weightW[]"
var legBands = index == 0 ? "legBandM[]" : "legBandW[]"
var wingBands = index == 0 ? "wingBandM[]" : "wingBandW[]"
html = `<input type="text" name="${entryIDs}" value="${value.entryID}">
<input type="text" name="${players}" value="${value.entryName}">
<input type="text" name="${weights}" value="${value.weight}">
<input type="text" name="${legBands}" value="${value.legBand}">
<input type="text" name="${wingBands}" value="${value.wingBand}">
<input type="text" name="eventID" value="${value.eventID}">
`
})
}
})
}
document.getElementById("resulta").innerHTML = html //add html to div
},
}
});
});
uj5u.com熱心網友回復:
你的 Val 不是一個陣列,它是一個物件,所以你不能使用 forEach。
Object.values(val).forEach // should work for you
uj5u.com熱心網友回復:
我剛剛洗掉了 return nonmatched... 現在它只回傳匹配的玩家。
謝謝大家。
return { ...groups}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/372163.html
標籤:javascript 查询 阿贾克斯
下一篇:將列舉轉換為具有值的物件
