嵌套地图和过滤方法

时间:2016-08-29 05:24:54

标签: javascript

我试图用JavaScript包围函数式编程。我的目标是使用width=150height=200检索对象boxart项目。

Console.log(arr)

显示器

[ [ [ [Object] ], [ [Object] ] ],
[ [ [Object] ], [ [Object] ] ] ]

我不知道为什么我无法打印出来 实际价值。 ConcatAll方法用于展平数组。我正在完成函数式编程的reactivex.io教程。

Array.prototype.concatAll = function() {
var results = [];
this.forEach(function(subArray) {
	subArray.forEach(function(item){
  	results.push(item);
});
});

return results;
};

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": 654356453,
					"title": "Bad Boys",
					"boxarts": [
						{ width: 200, height: 200, url: "http://cdn-0.nflximg.com/images/2891/BadBoys200.jpg" },
						{ width: 150, height: 200, url: "http://cdn-0.nflximg.com/images/2891/BadBoys150.jpg" }

					],
					"url": "http://api.netflix.com/catalog/titles/movies/70111470",
					"rating": 5.0,
					"bookmark": [{ id: 432534, time: 65876586 }]
				}
			]
		},
		{
			name: "New Releases",
			videos: [
				{
					"id": 65432445,
					"title": "The Chamber",
					"boxarts": [
						{ width: 150, height: 200, url: "http://cdn-0.nflximg.com/images/2891/TheChamber150.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: 150, height: 200, url: "http://cdn-0.nflximg.com/images/2891/Fracture150.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 }]
				}
			]
		}
	];

var arr = movieLists.map(function(movieList) {
	return movieList.videos.map(function(video) {
		return video.boxarts.filter(function(boxart) {
			return boxart.width === 150;
		  });
	  });
 });

console.log(arr);

1 个答案:

答案 0 :(得分:-1)

好的,我明白了。 console.log(JSON.stringify(arr));正在我正在使用的环境repl.it中显示值。