underscoreに詳しくないので、勉強半分でソースコードを読む。
利用するバージョン
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のもの同士で分解する。