在数组长度超过10项之后删除后续数组项(即删除第10项之后的所有项)-JavaScript

gopyfrb3  于 2022-10-22  发布在  Java
关注(0)|答案(2)|浏览(216)

我有一个文件上传器,它为要上传的文件提供预览图像。有前端和后端验证,可防止在一次上传中提交超过10个文件。
我想做的是,当附加了10个以上的文件时,第10个以上图像的预览图像将从图像预览中删除(当前也有错误消息输出)。对于输入元素中的实际FileList文件,我有单独的功能,但应该能够使解决方案同时适用于这两个元素。
下面是问题的最小代码表示。如果需要,我可以显示完整的文件上传器代码,但有相当多的代码与此问题无关,这可能会让人困惑。

// Generate a preview <img> for each selected file
// the submitData.files array is generated from a change event listener on a file input elment

[...submitData.files].forEach(showFiles);
});

function showFiles(file) {

    let previewImage = new Image();
    previewImage.className = "img upload-preview-image";
    previewImage.src = URL.createObjectURL(file);

    // use decode() method that waits for individual preview images to be added when running validations

    previewImage.decode().then((response) => {

        // validations happen here for each image as part of the showFiles() function called in the forEach loop above

        let imgList = document.querySelectorAll('.upload-preview-image'), // redeclare under new var name inside promise

        if (imgList.length > 10) {

            /***somehow remove all images from the imgList array after the 10th image,
            either by deleting them or exiting this function when the 10th item is added***/

        }

    }).catch((encodingError) => {
        // Do something with the error.
    });

}
bybem2ql

bybem2ql1#

如果只想处理输入中的前10个文件,那么可以将它们从submitData.files数组中切掉:

[...submitData.files].slice(0, 10).forEach(showFiles)
bqujaahr

bqujaahr2#

要从页面中删除imgList(非活动节点列表)中除前10个节点外的所有选定节点,只需遍历列表并在相应项上调用.remove()即可。

let imgList = document.querySelectorAll('.upload-preview-image')
const img_max = 10

for (const [i, el] of [...imgList].entries()) {
  if (i >= img_max) {
    console.log(`remove imgList[${i}] : ${el.innerText}`)
    el.remove()
  } else {
    console.log(`keep imgList[${i}] : ${el.innerText}`)
  }
}

// refresh the static nodelist
imgList = document.querySelectorAll('.upload-preview-image')
console.log(imgList.length)
<ol>
  <li class="upload-preview-image">
    image 1
  </li>
  <li class="upload-preview-image">
    image 2
  </li>
  <li class="upload-preview-image">
    image 3
  </li>
  <li class="upload-preview-image">
    image 4
  </li>
  <li class="upload-preview-image">
    image 5
  </li>
  <li class="upload-preview-image">
    image 6
  </li>
  <li class="upload-preview-image">
    image 7
  </li>
  <li class="upload-preview-image">
    image 8
  </li>
  <li class="upload-preview-image">
    image 9
  </li>
  <li class="upload-preview-image">
    image A
  </li>
  <li class="upload-preview-image">
    image B
  </li>
  <li class="upload-preview-image">
    image C
  </li>
  <li class="upload-preview-image">
    image D
  </li>
  <li class="upload-preview-image">
    image E
  </li>
  <li class="upload-preview-image">
    image F
  </li>
</ol>

或者,您可以只循环浏览需要删除的内容:

let imgList = document.querySelectorAll('.upload-preview-image')
const img_max = 10

for (const [i, el] of [...imgList].slice(img_max).entries()) {
  console.log(`remove ${el.innerText}`)
  el.remove()
}

// refresh the static nodelist
imgList = document.querySelectorAll('.upload-preview-image')
console.log(imgList.length)
<ol>
  <li class="upload-preview-image">
    image 1
  </li>
  <li class="upload-preview-image">
    image 2
  </li>
  <li class="upload-preview-image">
    image 3
  </li>
  <li class="upload-preview-image">
    image 4
  </li>
  <li class="upload-preview-image">
    image 5
  </li>
  <li class="upload-preview-image">
    image 6
  </li>
  <li class="upload-preview-image">
    image 7
  </li>
  <li class="upload-preview-image">
    image 8
  </li>
  <li class="upload-preview-image">
    image 9
  </li>
  <li class="upload-preview-image">
    image A
  </li>
  <li class="upload-preview-image">
    image B
  </li>
  <li class="upload-preview-image">
    image C
  </li>
  <li class="upload-preview-image">
    image D
  </li>
  <li class="upload-preview-image">
    image E
  </li>
  <li class="upload-preview-image">
    image F
  </li>
</ol>

相关问题