不能与文件列表一起使用forEach [英] Can't use forEach with Filelist
本文介绍了不能与文件列表一起使用forEach的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试遍历Filelist
:
console.log('field:', field.photo.files)
field.photo.files.forEach(file => {
// looping code
})
如您所见,field.photo.files
具有一个Filelist
:
如何正确遍历field.photo.files
?
推荐答案
FileList
不是Array
,但确实符合其合同(具有length
和数字索引),因此我们可以借用"Array
方法:
A FileList
is not an Array
, but it does conform to its contract (has length
and numeric indices), so we can "borrow" Array
methods:
Array.prototype.forEach.call(field.photo.files, function(file) { ... });
由于您显然使用的是ES6,因此还可以使用新的Array.from
方法将其设置为正确的Array
:
Since you're obviously using ES6, you could also make it a proper Array
, using the new Array.from
method:
Array.from(field.photo.files).forEach(file => { ... });
这篇关于不能与文件列表一起使用forEach的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文