[英]Javascript - remove empty or false items from array
我在服務器上使用 php 解碼文件。 當響應提供給客戶端並且我使用v-if
在我的 vue 應用程序中循環它時,我遇到了一個奇怪的行為,它將正確循環所有項目並顯示在屏幕上(我是以數據 uri 格式循環圖像)但在循環內我總是會得到兩個false
的項目,所以在 output 上我將有兩個空白圖像標簽。 我不確定這是否是由循環數據 URI 引起的,但有沒有辦法從數組中刪除錯誤項目?
這是客戶端代碼
axios.request({
url: 'https://localhost:3000/api/readfile',
method: 'GET',
headers: {
'Authorization': `Bearer ${this.$store.getters.userToken}`
},
data: formData
}).then( (response) => {
console.log(response);
response.data.master_file.forEach( (item) => {
console.log(item);
this.payload.push(item);
});
this.step++;
this.isLoading = false;
});
您可以將Array.push()
包裝在 if 語句中以避免添加false
項目。
axios.request({
url: 'https://localhost:3000/api/readfile',
method: 'GET',
headers: {
'Authorization': `Bearer ${this.$store.getters.userToken}`
},
data: formData
}).then( (response) => {
console.log(response);
response.data.master_file.forEach( (item) => {
console.log(item);
if (item) {
this.payload.push(item);
}
});
this.step++;
this.isLoading = false;
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.