1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

TypeScriptでmapメソッドにオブジェクト型の値をコールバック関数に渡す方法

Last updated at Posted at 2025-04-27

以下のように書く

type User = {
  id: number;
  name: string;
  age: number;
};

const users: User[] = [
  { id: 1, name: "Alice", age: 25 },
  { id: 2, name: "Bob", age: 30 },
  { id: 3, name: "Charlie", age: 35 }
];

const getUserName = (u: User): string => {
  return u.name
}

const getUserAge = (u: User): number => {
  return u.age
}
console.log(users.map(getUserName));
console.log(users.map(getUserAge));

初歩的だけど、引数()の後に:を付けて戻り値を宣言する
https://typescriptbook.jp/reference/functions/function-type-declaration

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?