Skip to content

Instantly share code, notes, and snippets.

@danielpetrov
Created January 10, 2017 09:13
Show Gist options
  • Save danielpetrov/67a12b841897f6dc3d7330b8ace15bfe to your computer and use it in GitHub Desktop.
Save danielpetrov/67a12b841897f6dc3d7330b8ace15bfe to your computer and use it in GitHub Desktop.
var input = [
{
title: "Batman Begins",
year: 2005,
cast: [
"Christian Bale",
"Michael Caine",
"Liam Neeson",
"Katie Holmes",
"Gary Oldman",
"Cillian Murphy"
]
},
{
title: "The Dark Knight",
year: 2008,
cast: [
"Christian Bale",
"Heath Ledger",
"Aaron Eckhart",
"Michael Caine",
"Maggie Gyllenhal",
"Gary Oldman",
"Morgan Freeman"
]
},
{
title: "The Dark Knight Rises",
year: 2012,
cast: [
"Christian Bale",
"Gary Oldman",
"Tom Hardy",
"Joseph Gordon-Levitt",
"Anne Hathaway",
"Marion Cotillard",
"Morgan Freeman",
"Michael Caine"
]
}
];
const flatMap = (data, propName) => {
return data.reduce((acc, value) => {
value[propName].forEach(star => {
if (acc.indexOf(star) === -1) {
acc.push(star);
}
});
return acc;
}, []);
const data = [[1,2,3], [4,5,6], [7,8,9]]
const flattenData = data => data.reduce((acc, value) => acc.concat(value),[] )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment