Can't use forEach with Filelist Can't use forEach with Filelist javascript javascript

Can't use forEach with Filelist


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

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 => { ... });


You can also iterate with a simple for:

var files = field.photo.files;for (var i = 0; i < files.length; i++) {    console.log(files[i]);}


In ES6 you can use:

[...field.photo.files].forEach(file => console.log(file));

Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment