let images = [{ galleryId: 1, name: 'dog', url: 'http://dog.com/fido.png'}, { galleryId: 1, name: 'cat', url: 'http://dog.com/hated.png' }, { galleryId: 2, name: 'polka', url: 'http:/dog.com/dancing.png' }];
let imagesByGallery = images.reduce(
(obj, image) => {
if (!obj.hasOwnProperty[image.galleryId]) {
obj[image.galleryId] = [];
}
obj[image.galleryId].push(image);
return obj;
},
{}
);