Pot.items

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

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

引数 object を 2 つのアイテムからなる配列をもつ配列 (items 形式) に変換して返します。

オブジェクト object を 2 つのアイテムからなる配列をもつ配列 (items 形式) にして返します。
引数 object は、オブジェクトまたは配列を指定します。
引数 callback は、任意にイテレートする関数を指定します。
引数 context が指定されると、callback 関数の this が context にとって代わります。

var obj = {
      foo : 1,
      bar : 2,
      baz : 3
};
debug(items(obj));
// [
//   ['foo', 1],
//   ['bar', 2],
//   ['baz', 3]
// ]

このように変換されます。
このリファレンスでは、上の例の結果のような 2 つのアイテムを持つ 2 次元配列を 「items 形式」としています。

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

function (item, object) { this === context }

    item   : {Array}      [key, value] とするアイテムの配列。index 0 -> key.
    object : {Object|*}   object 自身。

コールバックを使用する時、例えば 以下のように key, value が得られます。

var keys = [];
var values = [];

Pot.items({a: 1, b: 2, c: 3}, function(item, object) {
    keys.push(item[0]);
    values.push(item[1]);
});
Pot.debug(keys);   // ['a', 'b', 'c']
Pot.debug(values); // [1, 2, 3]

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

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

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

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

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

引数 object の 2 つのアイテムからなる配列をもつ配列 (items 形式) が返ります。

Pot.items で同期処理の例:

// オブジェクトへの例

var obj = {foo: 1, bar: 2, baz: 3};
debug(Pot.items(obj)); // [['foo', 1], ['bar', 2], ['baz', 3]]
// 配列への例

var array = ['foo', 'bar', 'baz'];
debug(Pot.items(array)); // [[0, 'foo'], [1, 'bar'], [2, 'baz']]
// コールバック関数を使った例 (配列)

var arr = ['foo', 'bar', 'baz'];

var result = Pot.items(arr, function(item, array) {
  return '(' + item[0] + ')' + item[1];
});
debug(result); // ['(0)foo', '(1)bar', '(2)baz']
// コールバック関数を使った例 (オブジェクト)

var obj = {foo: 1, bar: 2, baz: 3};

var result = Pot.items(obj, function(item) {
  return [item[0] + '::' + item[1]];
});
debug(result); // [['foo::1'], ['bar::2'], ['baz::3']]

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

// オブジェクトへの例

var obj = {foo: 1, bar: 2, baz: 3};

Pot.Deferred.items(obj).then(function(result) {
    debug(result); // [['foo', 1], ['bar', 2], ['baz', 3]]
});
// 配列への例

var array = ['foo', 'bar', 'baz'];

Pot.Deferred.items(array).then(function(result) {
    debug(result); // [[0, 'foo'], [1, 'bar'], [2, 'baz']]
});
// コールバック関数を使った例 (配列)

var arr = ['foo', 'bar', 'baz'];

Pot.Deferred.items(arr, function(item, array) {
    return '(' + item[0] + ')' + item[1];
}).then(function(result) {
    debug(result); // ['(0)foo', '(1)bar', '(2)baz']
});
// コールバック関数を使った例 (オブジェクト)

var obj = {foo: 1, bar: 2, baz: 3};

Pot.Deferred.items(obj, function(item) {
    return [item[0] + '::' + item[1]];
}).then(function(result) {
    debug(result); // [['foo::1'], ['bar::2'], ['baz::3']]
});

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

// オブジェクトへの例

var obj = {foo: 1, bar: 2, baz: 3};

var d = new Pot.Deferred();

d.then(function() {
    return obj;
}).items().then(function(result) {
    debug(result); // [['foo', 1], ['bar', 2], ['baz', 3]]
}).begin();
// 配列への例

var array = ['foo', 'bar', 'baz'];

var d = new Pot.Deferred();

d.items().then(function(result) {
    debug(result); // [[0, 'foo'], [1, 'bar'], [2, 'baz']]
}).begin(array);
// コールバック関数を使った例 (配列)

var arr = ['foo', 'bar', 'baz'];

Pot.Deferred.begin(function() {
    return arr;
}).items(function(item, array) {
    return '(' + item[0] + ')' + item[1];
}).then(function(result) {
    debug(result); // ['(0)foo', '(1)bar', '(2)baz']
});
// コールバック関数を使った例 (オブジェクト)

var obj = {foo: 1, bar: 2, baz: 3};

Pot.Deferred.begin(function() {
    return obj;
}).items(function(item) {
    return [item[0] + '::' + item[1]];
}).then(function(result) {
    debug(result); // [['foo::1'], ['bar::2'], ['baz::3']]
});