简体   繁体   English

JavaScript:为什么嵌套的reduce函数不能链接?

[英]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.

相关问题 我不明白为什么我在 a.reduce javascript 方法之后收到这个 output - I can't understand why I'm receiving this output after a .reduce javascript method 为什么我不能在切片方法之后链接方括号? - why I can't chain square braces after slice method? JavaScript 高级减少 function 我无法理解{}[迭代] - JavaScript advanced reduce function I can't get my head around {}[iteration] 为什么我不能在可观察对象上链接.map()函数 - Why can't i chain the .map() function on observables 为什么我的 JavaScript 替换功能无法正常工作? - Why can't I get my JavaScript replace function to work? 为什么我在a.reduce() function 中不能返回压入累加器的结果? - Why can't I return the result of pushing to the accumulator in a .reduce() function? 我无法在Javascript中使用我的函数 - I can't use my function in Javascript 为什么简单的MVC教程未调用我的javascript函数? 为什么我也不能调试? - Why is my javascript function not being invoked for simple MVC tutorial? Why can't I debug either? 我的javascript函数似乎无法正常工作。 我不知道为什么 - My javascript function doesn't seem to be working. I can't figure out why JavaScript:为什么我不能将 Array.prototype.filter 与.push() 链接在一起? - JavaScript: why can't I chain Array.prototype.filter with .push()?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM