Help us understand the problem. What is going on with this article?

linq.js メモ

More than 1 year has passed since last update.

会社でlinq.jsを使っているので紹介とよく使うライブラリのメモ

LINQとは?

LINQとは統合言語クエリ (LINQ: Language-Integrated Query)の略で
C#言語などに使われる配列操作を簡単にすることができものです。
簡単に言うとforeachの上位互換です。

linq.jsはそのLINQをjavaScriptに移植したライブラリです。
http://neue.cc/reference.htm

使い方

# install
npm install linq 


#import
import linq from 'linq'

よく使うメソッド

データ

const user = new Object(
    [
        {'id': 1, 'name': "tanaka", 'age': 24 },
        {'id': 2, 'name': "takahasi", 'age': 31 },
        {'id': 3, 'name': "suzuki", 'age': 29 },
        {'id': 4, 'name': "satou", 'age': 35 },
    ]
);

let user2 = new Object();

フィルター処理 [where]

const where = linq.from(user).where(x => x.age < 30).toArray()
console.log(where);
/* [{id: 1, name: "tanaka", age: 24},
** {id: 3, name: "suzuki", age: 29}]
*/

配列のすべての値に関数を適用したものを取得 [select]

const select = linq.from(user).select(x => x.age + 1).toArray()
console.log(select);
// [25, 32, 30, 36]

配列の先頭を取得 [first]

const first = linq.from(user).first()
console.log(first);
// {id: 1, name: "tanaka", age: 24}

先頭から何個か取得 [take]

const take = linq.from(user).take(2).toArray()
console.log(take);
/* [{id: 1, name: "tanaka", age: 24},
**  {id: 2, name: "takahasi", age: 31}]
*/

幾つかを飛ばして残りを取得 [skip]

const skip = linq.from(user).skip(2).toArray()
console.log(skip);
/* [{id: 3, name: "suzuki", age: 29},
**     {id: 4, name: "satou", age: 35}]
*/

先頭から条件を満たさなくなるまで取得(一度でも条件を満たさなくなったらストップ) [takeWhile]

const takeWhile = linq.from(user).takeWhile(x => x.age < 30).toArray()
console.log(takeWhile)
//{id: 1, name: "tanaka", age: 24}

条件を満たす最初の要素を取得する なければデフォルト(第二引数)を取得 [firstOrDefault]

const firsrOrDefault = linq.from(user).firstOrDefault(x => x.age < 30, 0)
const firsrOrDefault2 = linq.from(user).firstOrDefault(x => x.age < 10, 0)

console.log(firsrOrDefault);   //{id: 1, name: "tanaka", age: 24}
console.log(firsrOrDefault2);  //0

要素数数える [count]

const count = linq.from(user).count()
console.log(count)  //4

すべての要素が条件をみたすならtrue [all]

const all = linq.from(user).all(x => x.age > 20)
console.log(all);  //true

一つでも要素が条件をみたすならtrue [any]

const any = linq.from(user).any(x => x.name === 'suzuki')
console.log(any);  //true

リストが空か [any]

console.log(linq.from(user).any());   //true
console.log(linq.from(user2).any());  //false

最大値,最小値,平均値 [max, min, average]

console.log(linq.from(user).max(x => x.age) )  //35
console.log(linq.from(user).min(x => x.age) )  //24
console.log(linq.from(user).average( x => x.age)) //29.75

ソート [orderBy,orderByDescending]

//(昇順)
console.log(linq.from(user).orderBy(x => x.age).toArray());
//(降順)
console.log(linq.from(user).orderByDescending(x => x.age).toArray());

まとめ

  • laravel使っている人はモデルにLINQが使われているので使いやすい。
  • ネストが深くなるのを防げる
  • 可読性アップ!
  • スタイリッシュに書ける

皆さんも是非使ってみてください

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away