我知道如何分别做这两件事,但我确信必须有一种方法可以将它们结合起来。
我有一个类别数组,我从一组对象中提取:
this.videoCategories = this.videos.map(v => v.category);
但当然,这个数组中有重复项。所以现在我做
this.uniqueVideoCategories = this.videoCategories.filter((item, index) => {
return this.videoCategories.indexOf(item) === index;
});
效果很好,我得到了一系列没有欺骗的类别。但是我试图通过将它们串在一起来学习和干燥代码,但这不起作用 - 产生空数组
constructor(private videoService: VideoService) {
this.videos = videoService.getVideos();
this.videoCategories = this.videos
.map(v => v.category)
.filter((item, index) => {
return this.videoCategories.indexOf(item) === index;
});
console.log(this.videoCategories);
}