0
0

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コードリーディング(filter)

Last updated at Posted at 2015-12-07

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

利用するバージョン

underscore.js(v1.8.3)

filterとは

underscorejs.orgのfilter

こんな説明。

####_.filter(list, predicate, [context]) Alias: select
Looks through each value in the list, returning an array of all the values that pass a truth test (predicate).

var evens = _.filter([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });
=> [2, 4, 6]

selectにエイリアスが張られている。
listのすべての値に対してテストをし、そのテストを通ったもので形成されたArrayを返します。

Array.prototype.filterも存在する(IE9以上で利用可なので、IE8サポートの場合などはまだこっちを使うといいかも。)

underscore.filter

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

_.filter = _.select = function(obj, predicate, context) {
  var results = [];
  predicate = cb(predicate, context);
  _.each(obj, function(value, index, list) {
    if (predicate(value, index, list)) results.push(value);
  });
  return results;
};

返り値用のArray resultsを作成。
処理自体は_.each(.forEach)に処理を任せていて、.eachで処理をする中で条件に引っかかったものに関してだけresultsに値をpushしている。

その結果を最終的にreturnしている

.eachを内部で実行しているので、.filterもArray/objectの両方で利用できる。

object(連想配列)で_.filterを実行した場合は、valueが Array で返ってくるので、その点だけ注意。

こんな感じ

filter.js
'use strict';
var _ = require( "underscore" );

var arr = { "a": 1, "b": 2, "c": 3 };

var hoge = _.filter( arr, function( num, key ) {
    return num >= 2;
});

console.log( hoge );
shell
$ node filter.js
[ 2, 3 ]
0
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?