0

In my TypeScript module, I have a series of array structures where each of them will hold separate data.

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']
];

I iterate through some other data and start populating these arrays in the following way;

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

Array declaration block at the top appears to be too verbose. Is there more elegant way of declaring these data structures?

3
  • @JohnWeisz - structure of the array will be same for all instances but they will hold separate sets of data. Commented Sep 26, 2018 at 8:56
  • var meta = ['Year', 'Month', 'Program', 'Region', 'Market', 'Country', 'Started', 'Completed']; var monthlySheetP = [].push(meta); A better way would be to create a function that generate an array containing the redundant metaData Commented Sep 26, 2018 at 8:57
  • You can also add the meta data at the end of all logic using array.unshift(['Year', 'Month', 'Program', 'Region', 'Market', 'Country', 'Started', 'Completed']) Commented Sep 26, 2018 at 9:04

1 Answer 1

2

Use a object to store your data:

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'],
//...
};

An then work with that object:

monthlySheet[dealer.buService].push(cells);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.