26
16

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 5 years have passed since last update.

Lodashのチェーン記法について

Last updated at Posted at 2017-04-21

##まえおき
Lodash はメソッドチェーンで書くことができる。
たまに書き方を忘れるのでメモ。

35歳を超える人の名前を取るケース

var users = [
  { 'name': 'barney', 'age': 36 },
  { 'name': 'fred',   'age': 40 },
  { 'name': 'pebbles', 'age': 1 }
];

##チェーンを使わないと

_.pick(_.filter(users, function(user) { return user.age > 35; }), 'name');

// => [ { 'name': 'barney' }, { 'name': 'fred' } ] 

##チェーンを使うと

_(users)
  .filter(function(user) { return user.age > 35; })
  .pick('name')
  .value();

// => [ { 'name': 'barney' }, { 'name': 'fred' } ] 

##個人的には
チェーンのほうが見やすい。

#チェーン記法の場合、最後に .value() つけないと中間オブジェクト返すことがあるので注意。

##参考
https://lodash.com/docs/4.17.4#prototype-chain

26
16
1

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
26
16

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?