[英]JavaScript: Why can't I chain after my nested reduce function?
How do I go up a level and return "id and title" pairs to my array? 如何上一个级别并将“ id和title”对返回到数组? I get an error when trying to use map after my reduce function.
在我的reduce函数之后尝试使用map时出现错误。
Array.prototype.mergeAll = function(){
return [].concat.apply([], this);
};
Array.prototype.flatMap = function(func){
return this.map(function(item){
return func(item);
}).mergeAll();
};
function test() {
var movieLists = [
{
name: "New Releases",
videos: [
{
"id": 70111470,
"title": "Die Hard",
"boxarts": [
{ width: 150, height:200, url:"http://cdn-0.nflximg.com/images/2891/DieHard150.jpg" },
{ width: 200, height:200, url:"http://cdn-0.nflximg.com/images/2891/DieHard200.jpg" }
],
"url": "http://api.netflix.com/catalog/titles/movies/70111470",
"rating": 4.0,
"bookmark": []
},
{
"id": 654356453,
"title": "Bad Boys",
"boxarts": [
{ width: 200, height:200, url:"http://cdn-0.nflximg.com/images/2891/BadBoys200.jpg" },
{ width: 140, height:200, url:"http://cdn-0.nflximg.com/images/2891/BadBoys140.jpg" }
],
"url": "http://api.netflix.com/catalog/titles/movies/70111470",
"rating": 5.0,
"bookmark": [{ id:432534, time:65876586 }]
}
]
},
{
name: "Thrillers",
videos: [
{
"id": 65432445,
"title": "The Chamber",
"boxarts": [
{ width: 130, height:200, url:"http://cdn-0.nflximg.com/images/2891/TheChamber130.jpg" },
{ width: 200, height:200, url:"http://cdn-0.nflximg.com/images/2891/TheChamber200.jpg" }
],
"url": "http://api.netflix.com/catalog/titles/movies/70111470",
"rating": 4.0,
"bookmark": []
},
{
"id": 675465,
"title": "Fracture",
"boxarts": [
{ width: 200, height:200, url:"http://cdn-0.nflximg.com/images/2891/Fracture200.jpg" },
{ width: 120, height:200, url:"http://cdn-0.nflximg.com/images/2891/Fracture120.jpg" },
{ width: 300, height:200, url:"http://cdn-0.nflximg.com/images/2891/Fracture300.jpg" }
],
"url": "http://api.netflix.com/catalog/titles/movies/70111470",
"rating": 5.0,
"bookmark": [{ id:432534, time:65876586 }]
}
]
}
];
// Use one or more flatMap, map, and reduce calls to create an array with the following items (order doesn't matter)
// [
// {"id": 675465,"title": "Fracture","boxart":"http://cdn-0.nflximg.com/images/2891/Fracture120.jpg" },
// {"id": 65432445,"title": "The Chamber","boxart":"http://cdn-0.nflximg.com/images/2891/TheChamber130.jpg" },
// {"id": 654356453,"title": "Bad Boys","boxart":"http://cdn-0.nflximg.com/images/2891/BadBoys140.jpg" },
// {"id": 70111470,"title": "Die Hard","boxart":"http://cdn-0.nflximg.com/images/2891/DieHard150.jpg" }
// ];
return movieLists.flatMap(function(movieList) {
return movieList.videos.flatMap(function(vids){
return vids.boxarts.reduce(function(a,b){
if (a.width * a.height < b.width * b.height) return a;
else return b;
});
});
});
}
test();
// this is what gets returned... I want to add higher level object properties also. //这就是返回的内容...我也想添加更高级别的对象属性。 Specifically ID and Title.
特别是ID和标题。
=> [ { width: 150,
height: 200,
url: 'http://cdn-0.nflximg.com/images/2891/DieHard150.jpg' },
{ width: 140,
height: 200,
url: 'http://cdn-0.nflximg.com/images/2891/BadBoys140.jpg' },
{ width: 130,
height: 200,
url: 'http://cdn-0.nflximg.com/images/2891/TheChamber130.jpg' },
{ width: 120,
height: 200,
url: 'http://cdn-0.nflximg.com/images/2891/Fracture120.jpg' } ]
Here's a version that I think will work: 这是我认为可以使用的版本:
return movieLists.flatMap(function (movieList) {
return movieList.videos.flatMap(function (vids) {
return {
id: vids.id,
title: vids.title,
boxart: vids.boxarts.reduce(function (a, b) {
if (a.width * a.height < b.width * b.height) return a;
else return b;
}).url
}
});
});
You have access to the video info in that inner flatmap call, so this is where you need to construct your custom returned object that has the id and title. 您可以在内部平面图调用中访问视频信息,因此需要在此处构造具有id和title的自定义返回对象。 It might be clearer to rename the
vids
parameter to video
or something. 将
vids
参数重命名为video
或其他名称可能更清楚。 I Hope this helps! 我希望这有帮助! Let me know if you need any clarification.
让我知道您是否需要任何澄清。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.