如何使用单个数组角度js创建2D数组

时间:2017-03-31 09:56:10

标签: angularjs arrays

我们如何从单个数组

创建2D数组
angular.forEach(angular.fromJson(value.resource_type_efforts), function (value1, key1) {
          $scope.data.resource_type.push(key1);
});

当我 console.log($ scope.data.resource_type)输出时: -

0 :  "Senior Software Engineer"
1 :  "Software Engineer"
2 :  "QA Engineer"
3 :  "UI Developer"
4 :  "UI Designer"
5 :  "UI Developer"
6 :  "QA Engineer"
7 :  "Software Engineer"

现在我需要在每4个值之后将该数组拆分为2-d数组。

0:
  0 :  "Senior Software Engineer"
  1 :  "Software Engineer"
  2 :  "QA Engineer"
  3 :  "UI Developer"
1:
  0 :  "UI Designer"
  1 :  "UI Developer"
  2 :  "QA Engineer"
  3 :  "Software Engineer"

请提出一些答案

2 个答案:

答案 0 :(得分:2)

您可以使用此little demo fiddle中的ProgressBar使用一个辅助函数来实现此目的。您可以将数字解析为StatusStrip。这个数字可以根据您的需要自行更改。它还会收集数组的其余部分,例如你的初始数组中有9个项目。

> fiddle demo

Array.slice

通过这种方式,这应该为你做到:

splitCount

答案 1 :(得分:0)

当键得到%4 == 0 push temp输出数组和空白临时数组时,在temp变量中取一个输出数组和内部循环推送值

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="myCtrl">

</div>

<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) 
{
    $scope.resource_type_efforts = 
    [
        'Senior Software Engineer',
        'Software Engineer',
        'QA Engineer',
        'UI Developer',
        'UI Designer',
        'UI Developer',
        'QA Engineer',
        'Software Engineer'
    ];

    $scope.output = [];
    var temp = [];
    angular.forEach(angular.fromJson($scope.resource_type_efforts), function (value1, key1) 
    {

        temp.push(value1);
        if((key1+1)%4==0)
        {
            $scope.output.push(temp);
            temp = [];
        }
    });
    console.log($scope.output);
});
</script>

</body>
</html>