0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

完走賞のQiitanぬいぐるみをお迎えするためにUnityでゲーム作ってみるAdvent Calendar 2023

Day 3

個人的よく使うC#とTypescriptで配列操作

Last updated at Posted at 2023-12-02

はじめに

C#とTypeScriptを並行してかいているとごっちゃになるため、配列操作比較。
C#の配列操作はLINQです。

今回はとりあえず特に使う4つ。

配列に要素が含まれているか、条件を満たす要素をチェック

C# ⇒ Any()

string[] words = new string[] { "Hoge", "Fuga", "てすと" };
bool result = words.Any(x => x == "てすと");

よく使うのは配列が空じゃないか調べたいとき

string[] words = new string[] {};
bool result = words.Any();

TypeScript ⇒ some()

const words = ['Hoge', 'Fuga', 'てすと'];
words.some(x => x === 'てすと');

配列の要素が全て条件を満たしているかどうかをチェック

条件を満たさない要素が見つかった時点でfalseを返す

C# ⇒ All()

int[] numbers = new int[] {1,2,3};
bool result = numbers.All(x => x > 0);

TypeScript ⇒ every()

const numbers = [1,2,3];
numbers.every(x => x > 0);

別の配列をつくる

息をするように使う

C# ⇒ Select()

var test = testModel.Select(x => x.Test);

TypeScript ⇒ map()

const test = testModel.map(x => x.Test);

条件に一致する要素だけを含む新しい配列をつくる

息をするように使うパート2

C# ⇒ FindAll or Where

FindAllはListでないと使えない。
FindAllはLINQではなく、Listクラス。
(のであまり使ってない…)
例はWhere。

string[] words = new string[] { "Hoge", "Fuga", "てすと" };
var result = words.Where(x => x == "てすと");

TypeScript ⇒ filter()

const words = ['Hoge', 'Fuga', 'てすと'];
const result = words.filter(x => x === 'てすと');

おわりに

最近ぜんぜん覚えられない😢

0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?