Hashaam Ahmed
Hashaam Ahmed

Reputation: 340

Use Split in Vue js inside a loop

I have an api calling data from db, one of column has Image file name stored. I want to split the file and get the last 3 letters of each filename after the DOT. I am stuck here:

     axios.get('/api/v1/chats/messages/' + this.chat).then(response => {

                this.result = response.data;
                this.details = this.result.data;

                for (var j = 0; j < this.details.length; j++) {
                    this.str = this.details[j].image;
                    this.details[j].image_type = this.str.split(".");
                }
                console.log(this.details)
            })
                .catch(error => {

                    toastr.error('Something went wrong', 'Error', {
                        positionClass: 'toast-bottom-right'
                    });

                    this.cancelAutoUpdate();
                });

Would really appreciate if someone could help me OR please let me know if theres any other way to get the file extension in vue js. Thanks :)

Upvotes: 1

Views: 3572

Answers (1)

Илья Зелень
Илья Зелень

Reputation: 8108

function split splits a string into an array by a separator. You got an array, but forgot to get the array element you are interested in.

You need to replace this.str.split("."); to this.str.split(".")[1]

const details = [
  {
    image: 'image1.jpg',
  },
  {
    image: 'image2.png',
  },
  {
    image: 'image3.gif',
  }
]

for (let detail of details) {
  let str = detail.image;
  detail.image_type = str.split(".")[1];
}

console.log(details)

Upvotes: 2

Related Questions