We have 4 separate 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. Question: I am trying to combine boxarts.filter().reduce() into one iteration boxarts.reduce() in other words integrate filter() into reduce() . Really important- using reduce() and not to change the rest of the code if it is possible. I am trying to understand the ins and outs of reduce() . Any one to help?
the part of the code I want to refactor:
[boxarts.filter(function (boxart) {
return boxart.videoId === video.id;
}).reduce(function (acc, curr) {
if (acc.width * acc.height < curr.width * curr.height) {
return acc
} else {
return curr
}
return acc
}, [])]
the whole code
const 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: 300, height: 200, url: "http://cdn-0.nflximg.com/images/2891/DieHard300.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 }
];
Array.zip = function (bookmark, boxart, combinerFunction) {
let counter,
results = [];
for (counter = 0; counter < Math.min(bookmark.length, boxart.length); counter++) {
results.push(combinerFunction(bookmark[counter], boxart[counter]));
}
return results;
};
// Kick off the timer
console.time('filter.reduce');
let arr1 = lists.map(function (list) {
return {
name: list.name,
videos:
videos.
filter(function (video) {
return video.listId === list.id;
}).
map(function (video) {
return Array.zip(
bookmarks.filter(function (bookmark) {
return bookmark.videoId === video.id;
}),
[boxarts.filter(function (boxart) {
return boxart.videoId === video.id;
}).reduce(function (acc, curr) {
if (acc.width * acc.height < curr.width * curr.height) {
return acc
} else {
return curr
}
return acc
}, [])],
function (bookmark, boxart) {
return { id: video.id, title: video.title, time: bookmark.time, boxart: boxart.url };
});
})
};
});
// End the timer, get the elapsed time
console.timeEnd('filter.reduce');
// //to enable deep level flatten use recursion with reduce and concat
let concatArr1 = (function flattenDeep(arr1) {
return arr1.reduce((acc, val) => Array.isArray(val) ? acc.concat(flattenDeep(val)) : acc.concat(val), []);
})(arr1);
console.log(JSON.stringify(concatArr1, null, 2))
Desired Output
SOLUTION
let arr1 = lists.map(function (list) {
return {
name: list.name,
videos:
videos.
filter(function (video) {
return video.listId === list.id;
}).
map(function (video) {
return Array.zip(
bookmarks.filter(function (bookmark) {
return bookmark.videoId === video.id;
}),
[boxarts.reduce(function (acc, curr) {
if (curr.videoId === video.id) {
if (acc.width * acc.height < curr.width * curr.height) {
return acc
} else {
return curr
}
}
return acc
}, [])],
function (bookmark, boxart) {
return { id: video.id, title: video.title, time: bookmark.time, boxart: boxart.url };
});
})
};
});
