I would like to display either one of two images in a modal based on an API response…unfortunately, the API returns a long string array. I need to be able to determine if the word "Congratulations" is in this array. So far, I’ve tried a couple basic things:
Here is an example API response:
{
id: 12,
message: ["1 bonus point!", "Congratulations", "You have leveled up!"]
}
console.log(response) // the full response prints to the console, no problem
console.log(response?.message) //undefined
console.log(reponse.message) //undefined
console.log(response["message"]) //undefined
I want to be able to do something like this:
setSuccess(response["message"].contains("Congratulations"))
I’m sure it will be some small syntax thing, but I’m been banging my head against the wall. Any help is appreciated, let me know what I should try!
2
Answers
I would join the array and than call include.
By using the includes() method, you can check if the array contains a specific value. If "Congratulations" is present in the array, it will return true, and if it’s not present, it will return false. This boolean value can then be used to set the success state as needed.