我有一个从 db 调用数据的 api,其中一列存储了图像文件名。我想拆分文件并在 DOT 之后获取每个文件名的最后 3 个字母。我被困在这里:
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();
});
如果有人能帮助我,我将不胜感激,或者如果有任何其他方法可以在 vue js 中获取文件扩展名,请告诉我。谢谢 :)
最佳答案
函数 split
通过分隔符将字符串拆分为数组。你得到了一个数组,却忘了获取你感兴趣的数组元素。
您需要将 this.str.split(".");
替换为 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)
关于javascript - 在循环内的 Vue js 中使用 Split,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52037030/