次のコードの[0]
前で使用する理由がわかりませんdish.id and dish.featured
。
import { Injectable } from '@angular/core';
import { Dish } from '../shared/dish';
import { DISHES } from '../shared/dishes';
@Injectable()
export class DishService {
constructor() { }
getDishes(): Dish[] {
return DISHES;
}
getDish(id: number): Dish {
return DISHES.filter((dish) => (dish.id === id))[0];
}
getFeaturedDish(): Dish {
return DISHES.filter((dish) => dish.featured)[0];
}
}
これはDish
クラスです:
import { Comment } from './comment';
export class Dish {
id: number;
name: string;
image: string;
category: string;
label: string;
price: string;
featured: boolean;
description: string;
comments: Comment[];
}
この.filter()を読んでください。理由dish.id and dish.featured
がある[0]
ので、最終的にはfilter
常に配列で結果を返します。
var words = ['a','spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];
const result = words.filter(word => word.length == 1);
console.log(result);
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加