Reputation: 63
I'm messing around with this open API that I found. I returns a random Ron Swanson quote each time you GET from it.
The problem is that each the response.data comes out like this:
[ "Fish, for sport only, not for meat. Fish meat is practically a vegetable." ]
with brackets surrounding the text - which I don't want. Ive tried using slice() to get rid of the brackets but it doesn't seem to work.
What very simple thing am I missing?
Upvotes: 2
Views: 252
Reputation: 4650
You can try to use .split() if you're dealing with strings
var myVar = '["Uvuvwevwevwe Onyetenyevwe Ugwemubwem Osas"]';
myVar2 = myVar.split("[")[1];
myVar3 = myVar2.split("]")[0];
The final variable would hold "Uvuvwevwevwe Onyetenyevwe Ugwemubwem Osas".
The [] on the split method denotes the index of the divided string where "aXb" when split using split("X"), a is index 0, b is index 1.
Upvotes: 1
Reputation: 191
Just did a quick google search to find the API you are using -- https://github.com/jamesseanwright/ron-swanson-quotes is what it looks like.
Instead of returning a string with brackets, this API returns an array with a single value! So no need to perform any string manipulation, just access response.data[0]
to get the string that you want.
Edit: Slice would have been the correct way to remove the brackets if they were a part of the response string. Good instinct!
Upvotes: 5
Reputation: 234795
It's an array. Use subscripting notation to get the first element:
var response = [ "Fish, etc." ];
var text = response[0]; // "Fish, etc." (without the brackets)
Upvotes: 3