I am currently developing a small application that connects to an API to display Magic: The Gathering cards on an HTML webpage. These cards should be searchable, but as I am still learning and in school for this subject, I need help creating a filter function.
So far, I have extracted the card names into a variable called "arr" and am attempting to filter or match them with a value from the search input field labeled "strInput".
document.getElementById("searchButton").addEventListener("click", function(e){
const parentDiv = document.getElementById("cards");
if (parentDiv.hasChildNodes() === true) {
removeStuff();
} else {
filteredCards("https://api.magicthegathering.io/v1/cards");
}
}, false)
displayCards("https://api.magicthegathering.io/v1/cards");
function filteredCards(url) {
fetch(url)
.then((resp) => resp.json())
.then((data) => {
let myInput = document.getElementById("search").value;
let strInput = '"' + myInput + '"';
let arr = [];
for (i in data.cards) {
let jsonValue = data.cards[i];
let name = jsonValue.name;
arr.push(name);
}
let result = strInput.match(arr);
console.log(result);
console.log(arr);
console.log(strInput);
});
};
console.log(arr); // returns NULL even though the string matches.