1

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 :)

1 Answer 1

2

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)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.