输出结果几乎正确,但我在尝试展开嵌套的boxart数组时遇到了问题。
Javascript数据:
var movieLists = {
name: "Instant Queue",
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:432534, time:65876586 }]
},
{
"id": 654356453,
"title": ....,
}];
期望输出:(使用.map()
,.filter()
,.concatAll()
函数,返回具有尺寸为150x200的封面图像的电影的id、标题和boxart:url)
// [
// {"id": 675465,"title": "Fracture","boxart":"http://cdn-0...." },
// {"id": 65432445,"title": "The Chamber","boxart":"http://cdn-0...." },
// {"id": 654356453,...}
// ];
当前输出:
// [ //boxart value is an array
// {"id": 675465,"title": "Fracture","boxart":["http://cdn-0...."]},
// {"id": 65432445,"title": "The Chamber","boxart":["http://cdn-0...."]},
// {"id": 654356453,...}
// ];
我的解决方案:
return movieLists.map(function (category) {
return category.videos.map(function (video) {
return {
id: video.id,
title: video.title,
boxart: video.boxarts.filter(function (boxartFeature) {
return boxartFeature.width === 150 && boxartFeature.height === 200;
})
.map(function (boxartProp) {
return boxartProp.url;
})
};
});
}).concatAll(); //Flattens nested array by 1 dimension (please see demo)
我知道我需要使用.concatAll()
函数来移除嵌套的boxart数组,但我似乎找不到它的位置。
请点击这里进行演示