I've this function in a module for to load a files as base64 and its attributes:
function getFiles(obj, type) {
let destinazione = type;
let files = obj.files;
let imagesDocs = [];
files.forEach(file => {
let reader = new FileReader();
reader.onload = function(ev) {
let result = this.result;
let f = {
name: file.name,
size: file.size,
type: file.type,
data: result
};
imagesDocs.push(f);
};
reader.readAsDataURL(file);
});
return imagesDocs;
}
export default getFiles;
In another module, I import fileArray module and use it as:
const resultArray = getFiles(e.target, type);
let newArray = [...resultArray];
console.log(newArray);
console.log show me an empty array, while resultArray contain more objects.
I tried too with:
let numbers = [1, 2, 3, 4];
let newnumbers = [...numbers];
and work fine.
Why?
Update:
Using the Anson Miu's code I resolved. The "files" variable, being filelist type must be converted to array type. I did it with [...files].
Thanks to all.