inGroupsOf() 配列をグループ分けして、要素が足りない場合はオプションで埋める
<script>
Array.prototype._each = function(iterator){
for(var i=0, l=this.length; i<l; i++){
iterator(this[i]);
}
};
Array.prototype.each = function(iterator){
var index = 0;
try{
this._each(function(v){
iterator(v, index++);
});
} catch(e){
if(e != $break) throw e;
}
};
Array.prototype.collect = function(iterator){
var iterator = iterator || function(x){return x};
var rs = [];
this.each(function(v, k){
rs.push(iterator(v, k));
});
return rs;
};
Array.prototype.eachSlice = function(number, iterator){
var index = -number, slices = [], array = this.collect();
while((index += number) < array.length)
slices.push(array.slice(index, index + number));
return slices.collect(iterator);
};
Array.prototype.inGroupsOf = function(number, fillWith){
fillWith = (fillWith === undefined) ? null : fillWith;
return this.eachSlice(number, function(slice){
while(slice.length < number) slice.push(fillWith);
return slice;
});
};
var $break = {};
var kekka = "10,15,20,30,35".split(',').inGroupsOf(3, 'nanasi');
console.log(kekka);
</script>