Skip to content

Instantly share code, notes, and snippets.

@bsitruk
Created November 20, 2015 14:15
Show Gist options
  • Save bsitruk/e8650c368a1a6c741978 to your computer and use it in GitHub Desktop.
Save bsitruk/e8650c368a1a6c741978 to your computer and use it in GitHub Desktop.
Functional programming: join two arrays by a key
function() {
var lists = [
{
"id": 5434364,
"name": "New Releases"
},
{
"id": 65456475,
name: "Thrillers"
}
],
videos = [
{
"listId": 5434364,
"id": 65432445,
"title": "The Chamber"
},
{
"listId": 5434364,
"id": 675465,
"title": "Fracture"
},
{
"listId": 65456475,
"id": 70111470,
"title": "Die Hard"
},
{
"listId": 65456475,
"id": 654356453,
"title": "Bad Boys"
}
];
return lists.map(function(list) {
return {
name: list.name,
videos:
videos.
filter(function(video) {
return video.listId === list.id;
}).
map(function(video) {
return {id: video.id, title: video.title};
})
};
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment