基本的なLINQ（javascript編）

# はじめに
javascriptで配列を検索するときにC#の気持ちでLINQを書いた結果、whereがエラーに…
と言うことがあったのでjavascriptでの書き方を纏めておきます。

https://qiita.com/masayasu_t/items/1cdad910d38a872b836e

https://qiita.com/masayasu_t/items/c053b5c67be0dcb3c76c

# LINQ（C# ⇔ javascript）
### 存在チェック(Any)

x.kind == 3が存在するか
```c#:C#
bool vegetableAny = m_vegetable.Any(x => x.kind == 3);
```
```javascript:javascript
const vegetableAny = m_vegetable.some(x => x.kind == 3);
```

### 件数取得(Count)
x.kind == 2が何個あるか
```c#:C#
int vegetableCount = m_vegetable.Count(x => x.kind == 2);
```
```javascript:javascript
const vegetableCount = m_vegetable.filter(x => x.kind == 2).length;
```

### 並び替え(OrderBy)
```c#:C#
// x.idの昇順に並び替え
var vegetableOrderBy = m_vegetable.OrderBy(x => x.id);
// x.idの降順に並び替え
var vegetableOrderByDescending = m_vegetable.OrderByDescending(x => x.id);
```
```javascript:javascript
// x.idの昇順に並び替え
const vegetableOrderBy = m_vegetable.sort((a, b) => {return a.id - b.id;});
// x.idの降順に並び替え
const vegetableOrderByDescending = m_vegetable.sort((a, b) => {return b.id - a.id;});
```

### 絞り込み(Where)
x.kind == 1のデータを絞り込み
```c#:C#
var vegetableWhere = m_vegetable.Where(x => x.kind == 1);
```
```javascript:javascript
const vegetableWhere = m_vegetable.filter(x => x.kind == 1);
```

### 最初の1件(First)
x.kind == 5のデータの最初の1件を取得
```c#:C#
var vegetableFirst = m_vegetable.First(x => x.kind == 5);
```
```javascript:javascript
const vegetableFirst = m_vegetable.find(x => x.kind == 5);
```

### 選択(Select)
isRoot（x.kind == 1の場合：true、それ以外の場合：false）を追加
```c#:C#
var vegetableSelect = m_vegetable.Select(x => new { x.id
                                                  , x.name
                                                  , x.kind
                                                  , isRoot = x.kind == 1 });
```
```javascript:javascript
const vegetableSelect = m_vegetable.map((x) => {return { id: x.id
                                                       , name: x.name
                                                       , kind: x.kind
                                                       , isRoot: x.kind == 1 }});
```

### 連番付与(Select)
```c#:C#
var vegetableSelectAddIndex = m_vegetable.Select((x, index) => new { index, x.id, x.name });
```
```javascript:javascript
const vegetableSelectAddIndex = m_vegetable.map((x, index) => {return { index, id: x.id, name: x.name }});
```

### 結合(Join)

```c#:C#
// 内部結合
var vegetableInnerJoin = m_kind.Join(m_vegetable
                                   , x => x.id, y => y.kind
                                   , (x, y) => new { y.id
                                                   , y.name
                                                   , kind_id = y.kind
                                                   , kind_name = x.name });
// 外部結合
var vegetableOuterJoin = m_kind.GroupJoin(m_vegetable, x => x.id, y => y.kind, (x, y) => new { x, y })
                               .SelectMany(z => z.y.DefaultIfEmpty(new m_vegetable { id = 0, name = "", kind = 0 }), (z, w) => new { w.id, w.name, kind_id = z.x.id, kind_name = z.x.name });
```
```javascript:javascript
// 内部結合
const vegetableInnerJoin = m_vegetable.flatMap((x) => {return m_kind.filter(y => y.id == x.kind).map((z) => {return { id: x.id, name: x.name, kind_id: z.id, kind_name: z.name } }); });
// 外部結合
//検討中
```


#### サンプルデータ
```javascript:javascript
const m_kind = [{ id: 1, name: '根菜類' }
               ,{ id: 2, name: '葉茎菜類' }
               ,{ id: 3, name: '果菜類' }
               ,{ id: 4, name: '香辛野菜' }
               ,{ id: 5, name: '果実的野菜' }];

const m_vegetable = [{ id: 101, name:'大根', kind:1 }
                    ,{ id: 102, name:'人参', kind:1 }
                    ,{ id: 103, name:'ジャガイモ', kind:1 }
                    ,{ id: 104, name:'ごぼう', kind:1 }
                    ,{ id: 201, name:'白菜', kind:2 }
                    ,{ id: 202, name:'キャベツ', kind:2 }
                    ,{ id: 203, name:'ほうれん草', kind:2 }
                    ,{ id: 301, name:'キュウリ', kind:3 }
                    ,{ id: 302, name:'トマト', kind:3 }
                    ,{ id: 303, name:'ピーマン', kind:3 }
                    ,{ id: 304, name:'カボチャ', kind:3 }
                    ,{ id: 305, name:'枝豆', kind:3 }
                    ,{ id: 501, name:'メロン', kind:5 }
                    ,{ id: 502, name:'スイカ', kind:5 }];
```