配列の配列を展開します。
var nested = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8]
];
Array.prototype.concat.apply([], nested);
// -> [0, 1, 2, 3, 4, 5, 6, 7, 8]
apply
が配列を引数として展開してくれるおかげで、以下と同じようなことになるためです。
[].concat(nested[0], nested[1], nested[2]);
// -> [0, 1, 2, 3, 4, 5, 6, 7, 8]
Concatenating an array of arrays in Coffeescript - Stack Overflow で知りました。これは便利。
Array を拡張するならこんな感じですね。
Array.prototype.flatten = function() {
return Array.prototype.concat.apply([], this);
};