I am currently developing a matching system for Player vs. Player battles and I need to ensure that the keys are appended correctly to my div element. Previously, I have successfully used append with keys before. Below is the code snippet:
const source = [{
entryID: 1,
entryName: 'player1',
weight: 1900,
},
{
entryID: 2,
entryName: 'player2',
weight: 1900,
},
...
];
// Function to combine based on weight
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);
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;
console.log(result);
After using the newCombine function, I now need assistance on how to create keys and append these results as textboxes. Please see the following image for reference: https://i.stack.imgur.com/tQ6WS.png
The provided code snippet works well when combining two data entries with the same weight. However, I'm facing challenges in applying this logic to cases where there is less than or greater than equal to a 15-weight difference between entries. Any help would be greatly appreciated. Thank you!
HTML
<div id="appendhere"></div>
AJAX
// Using Ajax to fetch data and perform operations
$(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 do I append the keys here?
}
}
});
});