简体   繁体   English

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

[英]Javascript - remove empty or false items from array

I'm decoding a file using php on the server.我在服务器上使用 php 解码文件。 When the response is served to the client and I'm using the v-if to loop it in my vue app I'm encountering a strange beahviour, it will loop all the items correctly and they are displayed on the screen (I'm looping images in data uri format) but inside the loop I will always get two false items so on the output I will have two blank image tags.当响应提供给客户端并且我使用v-if在我的 vue 应用程序中循环它时,我遇到了一个奇怪的行为,它将正确循环所有项目并显示在屏幕上(我是以数据 uri 格式循环图像)但在循环内我总是会得到两个false的项目,所以在 output 上我将有两个空白图像标签。 I'm not sure if this is caused from the looped data URI but is there any way to remove the false items from the array?我不确定这是否是由循环数据 URI 引起的,但有没有办法从数组中删除错误项目?

This is the client code这是客户端代码

      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;
      });

You can wrap the Array.push() in an if statement to avoid adding false items.您可以将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