I have some demo app that fetching data from chuck norris jokes api. I'm adding variable to the endpoint which represent 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 is a variable that get from the user the desired joke category, if the user don't choose a category they can load some random joke. In that case the endpoint is different -
https://api.chucknorris.io/jokes/random
The question is, how can I set the endpoint based on if statement, something like this:
if(selectedCat) {
fetch(someAddress)
} else {
fetch(anotherAddress)
}