I have a list of positions, I want to return the position that has given coordinates but Array.find
can return undefined
. How do I make sure I have everything I need.
type Direction = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 type File = Direction type Rank = Direction type Pos = [Direction, Direction] const dirs: Direction[] = [1,2,3,4,5,6,7,8] const files: File[] = dirs const ranks: Rank[] = dirs const poss: Pos[] = files.flatMap(file => ranks.map(rank => pos(file, rank))); function pos(file: Direction, rank: Direction): Pos { return poss.find(([_file, _rank]) => { file === _file && rank === _rank; }); }
https://stackoverflow.com/questions/66609150/how-to-be-sure-to-get-an-item-from-an-array-without-undefined March 13, 2021 at 09:04AM
没有评论:
发表评论