TypeScript-限制冗长代码块的数据结构和组织

时间:2018-09-26 08:49:42

标签: typescript

在TypeScript模块中,我具有一系列数组结构,其中的每个结构都将保存单独的数据。

var monthlySheetP = [
    ['Year', 'Month', 'Program', 'Region', 'Market', 'Country', 'Started', 'Completed']
];
var monthlySheetV = [
    ['Year', 'Month', 'Program', 'Region', 'Market', 'Country', 'Started', 'Completed']
];
var monthlySheetT = [
    ['Year', 'Month', 'Program', 'Region', 'Market', 'Country', 'Started', 'Completed']
];
var monthlySheetB = [
    ['Year', 'Month', 'Program', 'Region', 'Market', 'Country', 'Started', 'Completed']
];
var monthlySheetU = [
    ['Year', 'Month', 'Program', 'Region', 'Market', 'Country', 'Started', 'Completed']
];
var monthlySheetPV = [
    ['Year', 'Month', 'Program', 'Region', 'Market', 'Country', 'Started', 'Completed']
];
var monthlySheetPT = [
    ['Year', 'Month', 'Program', 'Region', 'Market', 'Country', 'Started', 'Completed']
];
var monthlySheetVT = [
    ['Year', 'Month', 'Program', 'Region', 'Market', 'Country', 'Started', 'Completed']
];
var monthlySheetPVT = [
    ['Year', 'Month', 'Program', 'Region', 'Market', 'Country', 'Started', 'Completed']
];

我遍历其他一些数据,并以以下方式开始填充这些数组;

if (dealer.buService == 'B') {
    monthlySheetB.push(cells);
} else if (dealer.buService == 'U') {
    monthlySheetU.push(cells);
} else if (dealer.buService == 'PVT') {
    monthlySheetPVT.push(cells);
}

顶部的数组声明块似乎太冗长。有没有更优雅的方法来声明这些数据结构?

1 个答案:

答案 0 :(得分:2)

使用对象存储数据:

<script>
var app = angular.module("theApp", []);
app.controller("theController", function($scope)
{
    $scope.string = "hello there!";
});
</script>
<div id = "div1" ng-app = "theApp" ng-controller = "theController">
    {{string}}<br>
    <input ng-model = "string"> 
</div>
<div id = "div2" ng-app = "theApp" ng-controller = "theController">
    {{string}}<br>
    <input ng-model = "string"> 
</div>

然后使用该对象:

var monthlySheet = {
    'P': ['Year', 'Month', 'Program', 'Region', 'Market', 'Country', 'Started', 'Completed'],
    'V': ['Year', 'Month', 'Program', 'Region', 'Market', 'Country', 'Started', 'Completed'],
    'T': ['Year', 'Month', 'Program', 'Region', 'Market', 'Country', 'Started', 'Completed'],
    'B': ['Year', 'Month', 'Program', 'Region', 'Market', 'Country', 'Started', 'Completed'],
    'U': ['Year', 'Month', 'Program', 'Region', 'Market', 'Country', 'Started', 'Completed'],
    'PV': ['Year', 'Month', 'Program', 'Region', 'Market', 'Country', 'Started', 'Completed'],
//...
};
相关问题