JavaScript:为什么我的嵌套reduce函数之后无法链接?

时间:2014-11-08 19:35:03

标签: javascript functional-programming

我如何升级并返回" 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' } ]

1 个答案:

答案 0 :(得分:0)

这是我认为可行的版本:

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
        }
    });
});

您可以访问该内部flatmap调用中的视频信息,因此您需要构建具有id和title的自定义返回对象。将vids参数重命名为video或其他内容可能更为清晰。我希望这有帮助!如果您需要任何澄清,请与我们联系。