繁体   English   中英

Javascript - 从数组中删除空项或假项

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM