私はTypescriptを初めて使っていて、最初のパイプを作成しようとしています。私はそれが複数のフィールドで作業しているが、私はそれに1つの問題がある - おそらくnull値を含むことができる1つのフィールドがあり、私はこのチェックを行うためにどの構文を使用するか分からない。ここに私のコードです:角2パイプ変換文字列に変換する前にヌルチェック
import { PipeTransform, Pipe } from '@angular/core';
import { IFixture } from '../shared/entities/fixture';
@Pipe({
name: 'fixtureFilter'
})
export class FixtureFilterPipe implements PipeTransform {
transform(value: IFixture[], filterBy: string): IFixture[] {
/** check filter string is passed in and if so convert to lower case */
filterBy = filterBy ? filterBy.toLocaleLowerCase() : null;
/** indexOf checks if string is found, else do not filter */
return filterBy ? value.filter((fixture: IFixture) =>
(fixture.homeTeam.teamName.toLocaleLowerCase().indexOf(filterBy) !== -1) ||
(fixture.awayTeam.teamName.toLocaleLowerCase().indexOf(filterBy) !== -1) ||
(fixture.week.weekNumber.toLocaleLowerCase().indexOf(filterBy) !== -1) ||
(fixture.homeTeamScore.toString().toLocaleLowerCase().indexOf(filterBy) !== -1) ||
(fixture.awayTeamScore.toString().toLocaleLowerCase().indexOf(filterBy) !== -1) ||
(fixture.tournament.tournamentName.substr(fixture.tournament.tournamentName.length - 4).indexOf(filterBy) !== -1))
: value;
}
}
homeTeamScoreとawayTeamScore上記nullにすることができ数値型であり、私はフィルタでそれらを使用するために、文字列に変換する必要があります。ヌルでない場合にのみ文字列に変換するために使用する正しい構文は何ですか?この行の 'ヌルの '
私が手にエラーが' プロパティを読み取ることができません' でのtoString:要求されたとして
(fixture.homeTeamScore.toString().toLocaleLowerCase().indexOf(filterBy) !== -1)
ここではいくつかのサンプルJSONデータである:
[{"fixtureId":8,"tournamentId":2,"tournament":{"tournamentId":2,"tournamentName":"League 2023","seasonId":18,"season":{"seasonId":18,"seasonName":"2023"}},"weekId":11,"week":{"weekId":11,"weekNumber":"11"},"awayTeamId":43,"awayTeam":{"teamId":43,"teamName":"New York Jets","logoImage":"http://url/app/assets/images/logos/nyjets.png","headerImage":"http://url/app/assets/images/headers/nyjets.png","coachImage":"http://url/app/assets/images/coaches/nyjets.png","cheerleaderImage":"http://url/app/assets/images/cheerleaders/nyjets.png","divisionId":7,"division":null,"coachId":81,"coach":null},"homeTeamId":32,"homeTeam":{"teamId":32,"teamName":"Houston Texans","logoImage":"http://url/app/assets/images/logos/houston.png","headerImage":"http://url/app/assets/images/headers/houston.png","coachImage":"http://url/app/assets/images/coaches/houston.png","cheerleaderImage":"http://url/app/assets/images/cheerleaders/houston.png","divisionId":6,"division":null,"coachId":87,"coach":null},"awayTeamScore":31,"homeTeamScore":16,"isOvertime":false},{"fixtureId":21,"tournamentId":2,"tournament":{"tournamentId":2,"tournamentName":"League 2023","seasonId":18,"season":{"seasonId":18,"seasonName":"2023"}},"weekId":2,"week":{"weekId":2,"weekNumber":"2"},"awayTeamId":38,"awayTeam":{"teamId":38,"teamName":"Miami Dolphins","logoImage":"http://url/app/assets/images/logos/miami.png","headerImage":"http://url/app/assets/images/headers/miami.png","coachImage":"http://url/app/assets/images/coaches/miami.png","cheerleaderImage":"http://url/app/assets/images/cheerleaders/miami.png","divisionId":7,"division":null,"coachId":82,"coach":null},"homeTeamId":43,"homeTeam":{"teamId":43,"teamName":"New York Jets","logoImage":"http://url/app/assets/images/logos/nyjets.png","headerImage":"http://url/app/assets/images/headers/nyjets.png","coachImage":"http://url/app/assets/images/coaches/nyjets.png","cheerleaderImage":"http://url/app/assets/images/cheerleaders/nyjets.png","divisionId":7,"division":null,"coachId":81,"coach":null},"awayTeamScore":21,"homeTeamScore":55,"isOvertime":false}]
はEDIT:固定します、
transform(value: IFixture[], filterBy: string): IFixture[] {
/** check filter string is passed in and if so convert to lower case */
filterBy = filterBy ? filterBy.toLocaleLowerCase() : null;
/** indexOf checks if string is found, else do not filter */
return filterBy ? value.filter((fixture: IFixture) =>
(fixture.homeTeam.teamName.toLocaleLowerCase().indexOf(filterBy) !== -1) ||
(fixture.awayTeam.teamName.toLocaleLowerCase().indexOf(filterBy) !== -1) ||
(fixture.week.weekNumber.toLocaleLowerCase().indexOf(filterBy) !== -1) ||
(fixture.homeTeamScore !== null ? fixture.homeTeamScore.toString().toLocaleLowerCase().indexOf(filterBy) !== -1 : false) ||
(fixture.awayTeamScore !== null ? fixture.awayTeamScore.toString().toLocaleLowerCase().indexOf(filterBy) !== -1 : false) ||
(fixture.tournament.tournamentName.substr(fixture.tournament.tournamentName.length - 4).indexOf(filterBy) !== -1))
: value;
}
どのようなエラーが表示されますか?あなたの投稿をjsonデータで更新してください。 – Aravind
このパイプを使用しているヌル – user517406
の 'toString'プロパティを読み取ることができません。 **エラーを更新して、この**サンプルjsonデータ**と一緒に**および** html **コンポーネントを生成してください** – Aravind