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

underscoreコードリーディング(pluck)

1
Posted at

underscoreに詳しくないので、勉強半分でソースコードを読む。

利用するバージョン

underscore.js(v1.8.3)

invokeとは

underscorejs.orgのpluck

こんな説明。

####_.pluck(list, propertyName)
A convenient version of what is perhaps the most common use-case for map: extracting a list of property values.

var stooges = [{name: 'moe', age: 40}, {name: 'larry', age: 50}, {name: 'curly', age: 60}];
_.pluck(stooges, 'name');
=> ["moe", "larry", "curly"]

mapを用いて何かをやる場合のもっともあり得るユースケース用の便利なバージョンである。
mapを用いてlistの値を抽出する

例示だとstoogeの中からnameをkeyに抽出したArrayが返ってきている

underscore.pluck

コード的にはこのあたり。

// Convenience version of a common use case of `map`: fetching a property.
  _.pluck = function(obj, key) {
    return _.map(obj, _.property(key));
  };

.mapの中で.property(key)に合致する値のみをreturnする。
そのため、keyにnameが入ってきた場合はnameの値のみで形成されたArrayが返却される。

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