[英]I can't understand why I'm receiving this output after a .reduce javascript method
[英]JavaScript: Why can't I chain after my nested reduce function?
如何上一個級別並將“ id和title”對返回到數組? 在我的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();
//這就是返回的內容...我也想添加更高級別的對象屬性。 特別是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' } ]
這是我認為可以使用的版本:
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
}
});
});
您可以在內部平面圖調用中訪問視頻信息,因此需要在此處構造具有id和title的自定義返回對象。 將vids
參數重命名為video
或其他名稱可能更清楚。 我希望這有幫助! 讓我知道您是否需要任何澄清。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.