-
-
Save 3stacks/97ed1d6b33e4e3332e696da77304b917 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For each image in the dataset, output this object: | |
// { src: "picture-3.jpg", caption: "Lorem Ipsum Dolor 2<br>Ut enim ad minim veniam, quis nostrud exercitation ullamco<br>2017" } | |
const data = [ | |
{ | |
"title": "Lorem Ipsum Dolor 1", | |
"description": "Ut enim ad minim veniam, quis nostrud exercitation ullamco.", | |
"year": "2018", | |
"images": [ | |
"picture-1.jpg", | |
"picture-2.jpg" | |
] | |
}, | |
{ | |
"title": "Lorem Ipsum Dolor 2", | |
"description": "Ut enim ad minim veniam, quis nostrud exercitation ullamco", | |
"year": "2017", | |
"images": ["picture-3.jpg"] | |
}, | |
] | |
// reduce takes two arguments. a function and an initial value. Initialise with empty array | |
// acc is accumulated value, curr is current item | |
const works = data.reduce((acc, curr) => { | |
// For each image, build an 'image object' | |
// I would use map, but that will return an array, so this is how you can get around using that flatten | |
curr.images.forEach(image => { | |
acc.push({ | |
src: image, | |
caption: `${curr.title}<br>${curr.description}<br>${curr.year}` | |
}); | |
}); | |
return acc; | |
}, []); | |
// don't need flattened works anymore! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
aha.. thanks so much! :)