Skip to content

Instantly share code, notes, and snippets.

@3stacks
Forked from duyenho/lightbox-data.js
Last active March 3, 2018 05:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 3stacks/97ed1d6b33e4e3332e696da77304b917 to your computer and use it in GitHub Desktop.
Save 3stacks/97ed1d6b33e4e3332e696da77304b917 to your computer and use it in GitHub Desktop.
// 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!
@duyenho
Copy link

duyenho commented Mar 3, 2018

aha.. thanks so much! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment