私は速度を含むマップポイントの配列を持っており、25ポイントごとに新しい配列を作成し、それらをチャンクと呼ばれる別の配列に挿入します。だから、これは私がやっているものです:配列のチャンクをJavaScript内の別の配列にプッシュ
var chunks = []; // the array of chunks
var tempArray = []; //used for storing current chunk
var currentLoop = 0; //used for checking how many items have been taken
for (var i = 0; i < gon.map_points.length; i++) {
if (currentLoop == 26) { // if the current items stored is above 25
chunks.push(tempArray); // push the chunk
currentLoop = 0; // reset the count
tempArray = []; // reset the chunk
}
tempArray.push(gon.map_points[i].speed); // add item into chunk
currentLoop++; // increase count
}
マップ点の配列は点の完全数(例えば、それは117かもしれない)ではないので、私は最後を取得することはできませんない限り、これは正常に動作します私のチャンク配列に17ポイントが追加されました。
合計項目に関係なく、配列を25ポイントにまとめる方法はありますか?
のようなチャンクを設定するために、スプライスを使用することができますループスルーします。あなたは回避策を行うことができますそのように –
アレイを別のアレイにコピーするためにスライスを使用 –
[分割アレイをチャンクに分割]の可能性があります(http://stackoverflow.com/questions/8495687/split-array-into-chunks) –