I am trying to create an object that requires mapping data from four arrays included in a function. The goal is to accomplish the new map using functional programming. Thus, for example, the methods map, reduce, forEach, concat, and filter would be allowed as well as custom functions.
I have arrived at a non functional solution using for-loops, which I've included below. However, I get stuck on a purely functional approach.
The data set and data for the desired outcome can be viewed independently at http://jhusain.github.io/learnrx/, though I've included the data below.
The objective is to re-map the data using functional programming so that it resembles the desired output (included below).
I would appreciate help re-mapping the data using a functional approach and starting from the data shown below.
The exact text of the problem states:
Exercise 26: Converting from Arrays to Deeper Trees
Let's try creating a deeper tree structure. This time we have 4 seperate arrays each containing lists, videos, boxarts, and bookmarks respectively. Each object has a parent id, indicating its parent. We want to build an array of list objects, each with a name and a videos array. The videos array will contain the video's id, title, bookmark time, and smallest boxart url. In other words we want to build the following structure:
The desired output is:
[
{
"name": "New Releases",
"videos": [
{
"id": 65432445,
"title": "The Chamber",
"time": 32432,
"boxart": "http://cdn-0.nflximg.com/images/2891/TheChamber130.jpg"
},
{
"id": 675465,
"title": "Fracture",
"time": 3534543,
"boxart": "http://cdn-0.nflximg.com/images/2891/Fracture120.jpg"
}
]
},
{
"name": "Thrillers",
"videos": [
{
"id": 70111470,
"title": "Die Hard",
"time": 645243,
"boxart": "http://cdn-0.nflximg.com/images/2891/DieHard150.jpg"
},
{
"id": 654356453,
"title": "Bad Boys",
"time": 984934,
"boxart": "http://cdn-0.nflximg.com/images/2891/BadBoys140.jpg"
}
]
}
]
The function that serves as the starting point follows below.
I've named the function "combine" so that we can call it. Also, I've included my solution, made up of for-loops, at the end of the function after the bookmarks array. My attempt at a functional solution only got this far:
return lists.map(function(list) {
return {
name: list.name,
videos:
videos.
filter(function(video) {
return video.listId === list.id;
}). // I got stuck at this point.
This is the starting set of data:
function combine() {
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"
}
],
boxarts = [
{ videoId: 65432445, width: 130, height:200, url:"http://cdn-0.nflximg.com/images/2891/TheChamber130.jpg" },
{ videoId: 65432445, width: 200, height:200, url:"http://cdn-0.nflximg.com/images/2891/TheChamber200.jpg" },
{ videoId: 675465, width: 200, height:200, url:"http://cdn-0.nflximg.com/images/2891/Fracture200.jpg" },
{ videoId: 675465, width: 120, height:200, url:"http://cdn-0.nflximg.com/images/2891/Fracture120.jpg" },
{ videoId: 675465, width: 300, height:200, url:"http://cdn-0.nflximg.com/images/2891/Fracture300.jpg" },
{ videoId: 70111470, width: 150, height:200, url:"http://cdn-0.nflximg.com/images/2891/DieHard150.jpg" },
{ videoId: 70111470, width: 200, height:200, url:"http://cdn-0.nflximg.com/images/2891/DieHard200.jpg" },
{ videoId: 654356453, width: 200, height:200, url:"http://cdn-0.nflximg.com/images/2891/BadBoys200.jpg" },
{ videoId: 654356453, width: 140, height:200, url:"http://cdn-0.nflximg.com/images/2891/BadBoys140.jpg" }
],
bookmarks = [
{ videoId: 65432445, time: 32432 },
{ videoId: 675465, time: 3534543 },
{ videoId: 70111470, time: 645243 },
{ videoId: 654356453, time: 984934 }
];
//My non-functional solution
sizeArr = [];
for(var i = 0; i < lists.length; i++){
lists[i].videos = [];
for(var j = 0; j < videos.length; j++){
if(videos[j].listId === lists[i].id){
lists[i].videos.push(videos[j]);
}
for(var k = 0; k < bookmarks.length; k++){
if(bookmarks[k].videoId === videos[j].id && videos[j].listId === lists[i].id){
videos[j].time = bookmarks[k].time;
}
for(var l = 0; l < boxarts.length; l++){
var size = boxarts[l].width * boxarts[l].height;
sizeArr.push(size);
sizeArr = sizeArr.sort(function(min, max){
if(min < max){
return min;
}
if(boxarts[l].videoId === videos[j].id && videos[j].listId === lists[i].id){
videos[j].boxart = boxarts[l].url;
}
});
}
}
}
delete lists[i].id;
}
return lists;
}
combine();
.forEach()over the videos you filtered, and for each video,.forEach()(or.reduce()) over theboxartarray and find the entry with thevideoIdmatching the currentvideo.idand assign itsurlproperty tovideo.boxart. Then do similarly withbookmarks.