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

1
Posted at

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

利用するバージョン

underscore.js(v1.8.3)

unzipとは

underscorejs.orgのunzip

こんな説明。

####_.unzip(*arrays)
The opposite of zip.
Given a number of arrays, returns a series of new arrays, the first of which contains all of the first elements in the input arrays, the second of which contains all of the second elements, and so on.
Use with apply to pass in an array of arrays.


_.unzip([['moe', 'larry', 'curly'], [30, 40, 50], [true, false, false]])
=> ["moe", 30, true], ["larry", 40, false], ["curly", 50, false]


zipの反対。

arraysを与えると新しい配列のシリーズを返します。
返される配列は、1つ目の配列はarraysで渡されたすべての配列の1つ目の要素で構成された配列になっていて、2つ目以降も同様です。

配列のまとまった配列を渡して使います。

underscore.unzip

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

  // Complement of _.zip. Unzip accepts an array of arrays and groups
  // each array's elements on shared indices
  _.unzip = function(array) {
    var length = array && _.max(array, getLength).length || 0;
    var result = Array(length);

    for (var index = 0; index < length; index++) {
      result[index] = _.pluck(array, index);
    }
    return result;
  };

arrayを引数に、arrayの中にネスト化されている配列を_.pluckを用いて同じindexのもの同士で分解する。

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?