I have the following code which can be slapped into a console if you want to replicate it.
let text = "Heavy Crossbow (Underwater)";
const allWeapons = ["Heavy Crossbow", "Heavy Crossbow (Underwater)", "Composite Longbow (+2 Str)"];
const lootedItems = text.split(';').map(item => ({ name: item.trim() }));
function getMatchingItems(lootedItems) {
const matchingItems = {
heavy_crossbow: 0,
heavy_crossbow_underwater: 0,
};
for (let i = 0; i < lootedItems.length; i++) {
const lootedItemName = lootedItems[i].name.toLowerCase();
for (let x = 0; x < allWeapons.length; x++) {
const weaponName = allWeapons[x].toLowerCase();
const escapedWeaponName = weaponName.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
if (lootedItemName.match(escapedWeaponName)) {
if (weaponName === "heavy crossbow") {
matchingItems.heavy_crossbow++;
} else if (weaponName === "heavy crossbow (underwater)") {
matchingItems.heavy_crossbow_underwater++;
}
}
}
};
console.log(matchingItems)
};
const matchingItems = getMatchingItems(lootedItems);
The issue I'm facing this time is that when text is "Heavy Crossbow" the heavy_crossbow is 1 as intended. However, when text is "Heavy Crossbow (Underwater)" both keys are 1.
Any ideas how I can solve this?