Here are some JSON examples of Pokemon Battles:
[
{
"battleID": "1",
"trainers": [
{
"LastName": "Ketchum",
"ForeName": "Ash"
},
{
"LastName": "Mason",
"ForeName": "Misty"
}
]
},
{
"battleID": "2",
"trainers": [
{
"LastName": "Mason",
"ForeName": "Misty"
},
{
"LastName": "Brock",
"ForeName": "Stuart"
},
{
"LastName": "Ian",
"ForeName": "Foster"
}
]
},
{
"battleID": "3",
"trainers": [
{
"LastName": "Brock",
"ForeName": "Stuart"
},
{
"LastName": "Ketchum",
"ForeName": "Ash"
}
]
}
]
Now, I aim to create a grid that counts the matches between different Pokemon trainers/players. Each match can have up to 4 players at once.
Ash Ketchum Misty Mason Brock Stuart Ian Foster
Ash Ketchum 2 1 1 0
Misty Mason 1 2 1 1
Brock Stuart 1 1 2 1
Ian Foster 0 1 1 1
Below is my code snippet:
class Trainer {
constructor(firstname, lastname) {
this.firstname = firstname;
this.lastname = lastname;
}
coBattles(trainer) {
var battles = 0;
jsonData.map(x => {
x.trainers.map(y => {
if (this.firstname === y.ForeName && this.lastname === y.LastName) {
x.trainers.map(z => {
if (trainer.firstname === z.ForeName && trainer.lastname === z.LastName)
battles++;
});
}
});
});
return battles;
}
}
var pokemonTrainers = [];
// Currently Undesirable as I want a 'unique' array of all participating trainers.
jsonData.forEach(x => {
x.trainers.forEach(y => {
var trainer = new Trainer(y.ForeName, y.LastName);
pokemonTrainers.push(trainer);
});
});
//Battles between Misty Mason and Brock Stuart
console.log(pokemonTrainers[1].coBattles(pokemonTrainers[3]));
//returns 1
I am seeking advice on optimizing this code in vanilla JS or with third-party libraries to efficiently handle large amounts of battle data (millions).