gpt4 book ai didi

javascript - 不能将 forEach 与文件列表一起使用

转载 作者:IT王子 更新时间:2023-10-29 02:47:52 24 4
gpt4 key购买 nike

我正在尝试遍历 Filelist:

console.log('field:', field.photo.files)
field.photo.files.forEach(file => {
// looping code
})

如您所见,field.photo.files 有一个 Filelist:

enter image description here

如何正确循环 field.photo.files

最佳答案

A FileList 不是 Array,但它确实符合其约定(具有 length 和数字索引),因此我们可以“借用“数组方法:

Array.prototype.forEach.call(field.photo.files, function(file) { ... });

既然你显然在使用 ES6,你也可以使用新的 Array.from 方法使它成为一个合适的 Array:

Array.from(field.photo.files).forEach(file => { ... });

关于javascript - 不能将 forEach 与文件列表一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40902437/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com