You can assign imageCollection outside of the forEach (but still inside of processImage()). Then in the redefinition of imageCollection inside of the forEach, spread the contents of each previous imageCollection array, followed by the contents of element.imageData. Like so:
//...
processImage() {
let imageCollection = [];
imageArray.forEach((element) => {
imageCollection = [...imageCollection, ...element.imageData];
//...
Full Code for Your Context
const imageArray = [
{
imageData: [
'https://via.placeholder.com/50',
'https://via.placeholder.com/60'
],
},
{
imageData: [
'https://via.placeholder.com/100'
],
},
{
imageData: [
'https://via.placeholder.com/150'
],
}
];
processImage() {
let imageCollection = [];
imageArray.forEach((element) => {
imageCollection = [...imageCollection, ...element.imageData];
console.log(imageCollection);
// Expected result
//['https://via.placeholder.com/50', 'https://via.placeholder.com/60', 'https://via.placeholder.com/100', https://via.placeholder.com/150]
});
console.log('ImageCollection',imageCollection);
}
Full Code for Running Outside of Your Context
const imageArray = [
{
imageData: [
'https://via.placeholder.com/50',
'https://via.placeholder.com/60'
],
},
{
imageData: [
'https://via.placeholder.com/100'
],
},
{
imageData: [
'https://via.placeholder.com/150'
],
}
];
function processImage() {
let imageCollection = [];
imageArray.forEach((element) => {
imageCollection = [...imageCollection, ...element.imageData];
console.log(imageCollection);
// Expected result
//['https://via.placeholder.com/50', 'https://via.placeholder.com/60', 'https://via.placeholder.com/100', https://via.placeholder.com/150]
});
console.log('ImageCollect',imageCollection);
}
processImage();