Pot.filter

{Object|Array|*} Pot.filter ({Object|Array|*} object, {Function} callback [, {Object|*} context])
{Pot.Deferred} Pot.Deferred.filter ({Object|Array|*} object, {Function} callback [, {Object|*} context])
{Pot.Deferred} Pot.Deferred.prototype.filter ({Function} callback [, {Object|*} context])

Pot.js と PotLite.js で利用可能。

引数 object の各値に、 与えられた関数 callback を実行し、
その戻り値が真 (true) となるすべての要素からなる 新しいオブジェクト/配列を生成して返します。

処理は、Array.prototype.filter と同じように実行されます。
引数 object には、対象の配列またはオブジェクトを指定します。
引数 callback は、実行する関数を指定します。
引数 context が指定されると、callback 関数の this が context にとって代わります。

コールバック関数の引数は以下のようになります。

function (value, key, object) { this === context }

    value  : {*}              object の各値
    key    : {String|Number|*}  object の各キー
    object : {Object|Array}     object 自身

処理は 同期処理、非同期処理、それぞれ異なります。

  • Pot.filter は同期処理になります。
    Pot.globalize() 適応済みの場合は filter() で実行できます。
  • Pot.Deferred.filter は非同期処理になり、Pot.Deferred のインスタンスを返します。
    Pot.globalize() 適応済みの場合は Deferred.filter() で実行できます。
  • Pot.Deferred.prototype.filter は、Pot.Deferred.prototype.async() によって 同期/非同期 が変化します。
    Pot.Deferred チェイン上で実行でき、Pot.Deferred のインスタンスを返します。

callback 関数の中で StopIteration (Pot.StopIteration) を throw することでループを止められます。

Pot.Deferred.filter.slow() のようにして速度の指定ができます。
非同期では、ほとんどの環境で速度指定が可能ですが、 同期での速度指定は一部の環境 (XUL 上など) のみになります。
指定できる名前は以下の表を参照ください。
デフォルトの速度は normal です。

値 / メソッド名 速度
limp 最も遅い
doze 遅い
slow 遅め
normal 通常
fast 速め
rapid 速い
ninja 最も速い

object の各値に、関数 callback を実行した結果が 真 (true) となる すべての要素の 新しいオブジェクト/配列を返します。

Pot.filter で同期処理の例:

// 配列の例

var arr = [12, 5, 8, 130, 44];

var result = Pot.filter(arr, function(value, index, array) {
    return (value >= 10);
});
debug(result); // [12, 130, 44]
// オブジェクトの例

var obj = {a: 1, b: 20, c: 7, d: 5, e: 27, f: 99};

var result = Pot.filter(obj, function(value, key, object) {
    return (value >= 10);
});
debug(result); // {b: 20, e: 27, f: 99}

Pot.Deferred.filter で非同期処理の例:

// 配列の例

var arr = [12, 5, 8, 130, 44];

Pot.Deferred.filter(arr, function(value, index, array) {
    return (value >= 10);
}).then(function(result) {
    debug(result); // [12, 130, 44]
});
// 配列の例 (2)

var arr = [12, 5, 8, 130, 44, 10];

Pot.Deferred.filter(arr, function(value, index, array) {
    return (value >= 10);
}).then(function(result) {
    debug(result); // [12, 130, 44, 10]
});
// オブジェクトの例

var obj = {a: 1, b: 20, c: 7, d: 5, e: 27, f: 99};

Pot.Deferred.filter(obj, function(value, key, object) {
    return (value >= 10);
}).then(function(result) {
    debug(result); // {b: 20, e: 27, f: 99}
});

Pot.Deferred.prototype.filter で非同期処理の例:

// 配列の例

var arr = [12, 5, 8, 130, 44];

var d = new Pot.Deferred();

d.then(function() {
    return arr;
}).filter(function(value, index, array) {
    return (value >= 10);
}).then(function(result) {
    debug(result); // [12, 130, 44]
}).begin();
// 配列の例 (2)

var arr = [12, 5, 8, 130, 44, 10];

var d = new Pot.Deferred();

d.filter(function(value, index, array) {
    return (value >= 10);
}).then(function(result) {
    debug(result); // [12, 130, 44, 10]
}).begin(arr);
// オブジェクトの例

var obj = {a: 1, b: 20, c: 7, d: 5, e: 27, f: 99};

Pot.Deferred.begin(function() {
    return obj;
}).filter(function(value, key, object) {
    return (value >= 10);
}).then(function(result) {
    debug(result); // {b: 20, e: 27, f: 99}
});