I am currently working on a demo app that retrieves data from the Chuck Norris jokes API. I have included a variable in the endpoint to specify the joke category.
fetchJoke: function() {
fetch(
`https://api.chucknorris.io/jokes/random?category=${this.selectedCat}`
)
.then(response => {
return response.json();
})
.then(jsonOBj => {
this.joke = jsonOBj.value;
})
.catch(error => {
console.log(error);
});
}
The selectedCat variable is used to capture the user's preferred joke category. If no category is chosen, a random joke is loaded. In such cases, the endpoint changes to -
https://api.chucknorris.io/jokes/random
Now, my question is how can I dynamically set the endpoint based on an if statement, similar to the following:
if(selectedCat) {
fetch(someAddress)
} else {
fetch(anotherAddress)
}