I am trying to create an object array that will contain objects that represent every scenario of combination possibilities that are provided by three string arrays. Each object in my Object Array (Collection) should contain the properties {format: '', iconType: '', state: ''} where each property can be a sting or a null value.
So in my code I have 3 string arrays... like so:
const buttonFormats = ['compact', 'regular', 'fluid'];
const stateParams = [
null,
'secondary',
'2',
'tertiary',
'3',
'warning',
'!',
'danger',
'!!!',
'danger-secondary',
'2!!!',
'primary-line',
'1l',
'secondary-line',
'2l',
'tertiary-line',
'3l',
'warning-line',
'!l',
'danger-line',
'!!!l'
];
const iconTypes = [null, 'add', 'edit'];
const allCombinations = [];
My plan is to have three nested loops where we take the current value of one loop and combine it with the values of the other loops, like so...
public makeButtonStates(): any[] {
const buttonFormats = ['compact', 'regular', 'fluid'];
const stateParams = [
null,
'secondary',
'2',
'tertiary',
'3',
'warning',
'!',
'danger',
'!!!',
'danger-secondary',
'2!!!',
'primary-line',
'1l',
'secondary-line',
'2l',
'tertiary-line',
'3l',
'warning-line',
'!l',
'danger-line',
'!!!l'
];
const iconTypes = [null, 'add', 'edit']; // these are the available icons
const allCombinations = [];
buttonFormats.forEach((format) => {
const currentCombinaton: any = {};
currentCombinaton.format = format;
iconTypes.forEach((icon) => {
currentCombinaton.iconType = icon;
stateParams.forEach((state) => {
currentCombinaton.state = state;
console.log(currentCombinaton);
allCombinations.push(currentCombinaton);
});
});
});
return allCombinations;
}
public ngOnInit(): void {
this.buttonStates = this.makeButtonStates();
console.log(this.buttonStates);
}
This isn't working as although when I output using console.log() the current combination of values what is written to the console is what I would expect, however when I write the outcome of my nested loop function, the value of this.buttonStates or allCombinations in the all the objects in the object array have the same value for the iconType and state properties, these being the last times in the string arrays ´{iconType: "edit", state: "!!!l"}` This screen shot of the developer console gives an idea of the output...
Obviously my implementation is wrong, should I use .map or .filter or .reduce instead and why is my output array only taking the last values of the stateParams and iconType array?
Many thanks in advance if you can help me spot this.
