I’m doing a matchmaking system where 2 players with the same level will be matched (Joined in 1 array). My target is how can I make the player who has no match to have a 2nd data in array?
JavaScript
x
3
1
Example: EntryID: “15”, player: ”testing11”, level: ”3”
2
EntryID: ”nm”, player: ”nm”, level: ”nm”;
3
The example I provided is when a player who doesn’t have a match, there should be another data that produces “nm” which means no match.
I have provided an image of my target for better visualization of my problem and target. Thank you so much
Script:
JavaScript
1
113
113
1
let ajaxResult = []; // the pushed data will be saved here
2
let save_method;
3
let table;
4
let base_url = "<?php echo base_url();?>";
5
let result = [];
6
var html = "";
7
8
9
10
11
// This is where the same level will be matched
12
const combine = (source) => {
13
return source.reduce((acc, curr) => {
14
if (acc[curr.level]) {
15
const levelArr = acc[curr.level];
16
const last = levelArr[levelArr.length - 1];
17
if (last.length === 2) {
18
levelArr.push([curr])
19
} else {
20
last.push(curr)
21
}
22
} else {
23
acc[curr.level] = [
24
[curr]
25
];
26
}
27
return acc;
28
}, {})
29
};
30
31
32
33
// I'm removing the duplicates here. Test 1 vs Test 1 shouldn't be possible
34
function removeDuplicates(result) {
35
return Object.values(result.reduce((acc, curr) => {
36
acc[curr.player] = acc[curr.player] || curr;
37
return acc;
38
}, {}))
39
}
40
41
42
43
const uniquePlayers = removeDuplicates(result);
44
45
46
$(document).ready(function() {
47
//datatables
48
table = $("#entry_list1").DataTable({
49
50
51
processing: false,
52
serverSide: true,
53
order: [],
54
searching: false,
55
paging: false,
56
info: false,
57
58
ajax: {
59
url: "<?php echo site_url('controller/fetch_players')?>",
60
type: "POST",
61
async: true,
62
dataType: "json",
63
64
success: function(data) {
65
66
67
result = combine(removeDuplicates(data.data2));
68
69
70
71
var keys = Object.keys(result)
72
console.log(JSON.stringify(data))
73
74
75
76
// I am creating a textbox depends on the matches above so that I can insert it on DB. My target here is to produce a textbox for my "no match" player. Because this currently code is only creating textboxes for matched players.
77
for (var i = 0; i < keys.length; i++) {
78
result[keys[i]].forEach(function(val) {
79
val.forEach(function(value, index) {
80
81
var entryIDs = index == 0 ? "entryIDM[]" : "entryIDW[]"
82
var players = index == 0 ? "playerM[]" : "playerW[]"
83
var levels = index == 0 ? "levelM[]" : "levelW[]"
84
html += `<input type="text" name="${entryIDs}" value="${value.entryID}">
85
<input type="text" name="${players}" value="${value.player}">
86
<input type="text" name="${levels}" value="${value.level}">
87
`
88
})
89
})
90
}
91
document.getElementById("result").innerHTML = html
92
93
94
95
96
97
98
},
99
},
100
101
"columnDefs": [{
102
"targets": [0], //first column
103
"orderable": false, //set not orderable
104
},
105
{
106
"targets": [-1], //last column
107
"orderable": false, //set not orderable
108
},
109
110
],
111
});
112
});
113
json.stringify
JavaScript
1
3
1
{"draw":"1","recordsTotal":5,"recordsFiltered":5,"data":[["15","testing11","3"],["13","testing8","1"],["4","testing4","2"],["3","testing3","2"],["1","testing1","1"]],"data2":[{"entryID":"15","player":"testing11","level":"3"},{"entryID":"13","player":"testing8","level":"1"},{"entryID":"4","player":"testing4","level":"2"},{"entryID":"3","player":"testing3","level":"2"},{"entryID":"1","player":"testing1","level":"1"}]}
2
3
Advertisement
Answer
If your target is only to add input for nm
you can check if the JSON Array length is 1
or not . If its 1
it means there is only one match found so you can add other match with nm
values inside val.forEach(..
.
Demo Code:
JavaScript
1
82
82
1
//just for demo....
2
var data = {
3
"data2": [{
4
"entryID": "15",
5
"player": "testing11",
6
"level": "3"
7
}, {
8
"entryID": "13",
9
"player": "testing8",
10
"level": "1"
11
}, {
12
"entryID": "4",
13
"player": "testing4",
14
"level": "2"
15
}, {
16
"entryID": "3",
17
"player": "testing3",
18
"level": "2"
19
}, {
20
"entryID": "1",
21
"player": "testing1",
22
"level": "1"
23
}, {
24
"entryID": "5",
25
"player": "testing5",
26
"level": "5"
27
}]
28
}
29
30
const combine = (source) => {
31
return source.reduce((acc, curr) => {
32
if (acc[curr.level]) {
33
const levelArr = acc[curr.level];
34
const last = levelArr[levelArr.length - 1];
35
if (last.length === 2) {
36
levelArr.push([curr])
37
} else {
38
last.push(curr)
39
}
40
} else {
41
acc[curr.level] = [
42
[curr]
43
];
44
}
45
return acc;
46
}, {})
47
};
48
49
function removeDuplicates(result) {
50
return Object.values(result.reduce((acc, curr) => {
51
acc[curr.player] = acc[curr.player] || curr;
52
return acc;
53
}, {}))
54
}
55
56
57
result = combine(removeDuplicates(data.data2));
58
var keys = Object.keys(result)
59
var html = ""
60
for (var i = 0; i < keys.length; i++) {
61
result[keys[i]].forEach(function(val) {
62
var length_ = val.length; //length of the json aray inside obj
63
val.forEach(function(value, index) {
64
65
var entryIDs = index == 0 ? "entryIDM[]" : "entryIDW[]"
66
var players = index == 0 ? "playerM[]" : "playerW[]"
67
var levels = index == 0 ? "levelM[]" : "levelW[]"
68
html += `<input type="text" name="${entryIDs}" value="${value.entryID}">
69
<input type="text" name="${players}" value="${value.player}">
70
<input type="text" name="${levels}" value="${value.level}">`
71
72
//if length is only one
73
if (length_ == 1) {
74
//just add inputs with nm..
75
html += `<input type="text" name="entryIDW[]" value="nm"> <input type="text" name="playerW[]" value="nm"><input type="text" name="levelW[]" value="nm">`
76
77
}
78
79
})
80
})
81
}
82
document.getElementById("result").innerHTML = html
JavaScript
1
2
1
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
2
<div id="result"></div>