[英]How to make keys for my console.log data and append it to html (Console log result to textboxes)
我正在制作匹配系統(玩家對玩家)我的目標是。 我如何確保將鍵附加到我的 div 中?
我之前已經用 append 制作了一個密鑰。 這是片段
const source = [{ entryID: 1, entryName: 'player1', weight: 1900, }, { entryID: 2, entryName: 'player2', weight: 1900, }, { entryID: 3, entryName: 'player3', weight: 1910, }, { entryID: 4, entryName: 'player4', weight: 1910, }, { entryID: 5, entryName: 'player5', weight: 1915, }, { entryID: 6, entryName: 'player6', weight: 1915, }, { entryID: 7, entryName: 'player7', weight: 1920, }, { entryID: 8, entryName: 'player8', weight: 1920, }, { entryID: 9, entryName: 'player9', weight: 1930, }, { entryID: 10, entryName: 'player10', weight: 1930, }, ] const combine = (source) => { return source.reduce((acc, curr) => { if (acc[curr.weight]) { const levelArr = acc[curr.weight]; const last = levelArr[levelArr.length - 1]; if (last.length === 2) { levelArr.push([curr]) } else { last.push(curr) } } else { acc[curr.weight] = [ [curr] ]; } return acc; }, {}) }; var result = combine(source) var html = "" var keys = Object.keys(result) //if there are more than one keys ie : 2.. for (var i = 0; i < keys.length; i++) { result[keys[i]].forEach(function(val) { val.forEach(function(value, index) { var entryIDs = index == 0 ? "entryIDM[]" : "entryIDW[]" var handlers = index == 0 ? "handlerM[]" : "handlerW[]" var weights = index == 0 ? "weightM[]" : "weightW[]" html += `<input type="text" name="${entryIDs}" value="${value.entryID}"> <input type="text" name="${handlers}" value="${value.entryName}"> <input type="text" name="${weights}" value="${value.weight}"> ` }) }) } document.getElementById("result").innerHTML = html //add html to div console.log(result);
<div id="result"> </div>
這些是我執行 newCombine 函數后的數據...現在我的目標是如何制作鍵並將這些結果附加為文本框?
我提供的代碼段在 2 個具有相同權重的數據時有效。 它們組合在 1 個數組中。 轉到我的目標,現在,我很難在我當前的函數中應用它,該函數在 2 個數據小於或大於等於 15 的權重差異時起作用。 請幫我。 非常感謝。
html
<div id="appendhere"> </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() {
var entry_list =$('#entry_list1').DataTable({
"ajax": {
"url": "<?php echo site_url('report/controlget')?>",
"type": "get",
success: function(data) {
const source = data;
const a = newCombine(source, 15);
console.log(a);
//How can i append my key here?
},
}
});
});
我將避免在這里使用與控制台和 div 相關的東西,以確保該解決方案可以以任何方式重用。 您可以隨心所欲地處理結果,包括將其添加到 div 或在控制台上顯示。
我們需要使用標准對項目進行分組,根據權重差異小於給定值,例如。 15.
讓我們考慮一下我們有 1900、1910 和 1920 等權重的例子。我們不能有(1900、1910 和 1920)這樣的權重組,因為 1920 - 1900 = 20 > 15。
我們可以有 (1900, 1910), (1920) 或 (1900), (1910, 1920) 之類的權重組
const source = [{ entryID: 1, entryName: 'player1', weight: 1900, }, { entryID: 2, entryName: 'player2', weight: 1900, }, { entryID: 3, entryName: 'player3', weight: 1910, }, { entryID: 4, entryName: 'player4', weight: 1910, }, { entryID: 5, entryName: 'player5', weight: 1915, }, { entryID: 6, entryName: 'player6', weight: 1915, }, { entryID: 7, entryName: 'player7', weight: 1920, }, { entryID: 8, entryName: 'player8', weight: 1920, }, { entryID: 9, entryName: 'player9', weight: 1930, }, { entryID: 10, entryName: 'player10', weight: 1930, }, ]; let groups = []; for (let item of source) { let found = false; for (let group of groups) { if (!found) { let isFit = true; for (let element of group) { if (Math.abs(element.weight - item.weight) > 15) isFit = false; } if (isFit) { group.push(item); found = true; } } } if (!found) groups.push([item]); } document.getElementById("foo").innerText = JSON.stringify(groups);
<div id="foo"></div>
我們循環元素並將每個元素迭代到第一個匹配的組中。 如果沒有任何組匹配,我們創建一個新組。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.