我有數字數組,如:一格創建一個二維數組
var data=['1','4','2','1','6','9','1','5',]
,我想將其轉換成二維數組,像下面這樣:
result = [['1','4','2','1'],['6','9','1','5']]
我的目標是使動態網格風格的菜單系統,離子從下面的例子中
<div class="text-center" ng-init="models = [['12','1','2','3','3'],['4','4','5','6','7']]">
<div ng-repeat="m in models">
<span class="control-label1" ng-repeat="movie in m track by $index">
{{movie}}</span>
</div>
</div>
爲了做到這一點,我需要在2維數組中添加我所有的值。我在給定函數如下
//this function make populate my array with unique value
$scope.addhourinArray = function (value) {
if (value) {
if ($scope.hourArray.indexOf(value) == -1) {
$scope.hourArray.push(value);
}
}
};
for (var i = 0; i < data.length; i++) {
$scope.addhourinArray(hour);
}
[將JS數組分割爲N個數組](http://stackoverflow.com/questions/8188548/splitting-a-js-array-into-n-arrays) – adeneo
使用lodash _.chunk – Ved