Search code examples
vue.jsaxiostwitchtwitch-api

Axios Response Data Replace


I'm trying to learn Axios and Vue with using Twitch API. I'm fetching data from that API and there is thumbnail_url which is for channel's thumbnail photos but I have to change that data's width and height because it's coming like this;

https://static-cdn.jtvnw.net/previews-ttv/live_user_shroud-{width}x{height}.jpg

And I was trying to do like this;

beforeMount(){
  helix.get('streams?language=en').then((response) => {
    for(var i=0; i < response.data.data.length; i++){
      response.data.data[i].thumbnail_url.replace("width", "40")
    }
    console.log(response.data.data)
  this.results = response.data.data
    })
    .catch(function (error) {
     console.log(error);
     });
},

Actually, I didn't understand what that is not working. I know there is a point that I missed. If someone can help me, It'd be great. And If It is not correct way to do this, what is the correct way? Thanks a lot.


Solution

  • You should use replace("{width}", "40"); instead

    var url = 'https://static-cdn.jtvnw.net/previews-ttv/live_user_shroud-{width}x{height}.jpg';
    url = url.replace("{width}", "40");
    url = url.replace("{height}", "60");
    console.log(url);

    In your code change this

    var thumbnail_url = response.data.data[i].thumbnail_url;
    thumbnail_url = thumbnail_url.replace("{width}", "40");
    thumbnail_url = thumbnail_url.replace("{height}", "60");
    response.data.data[i].thumbnail_url = thumbnail_url;
    

    As you comment you can do it without variable also

    response.data.data[i].thumbnail_url = response.data.data[i].thumbnail_url.replace("{width}", "40").replace("{height}", "60");