In my arrayList called selectedSources, I have items such as:
this.selectedSources.push(
{ id: 0, text: "A" },
{ id: 1, text: "B" },
{ id: 2, text: "C" },
{ id: 3, text: "D"}
);
The user has the option to select one or more of these items. When items are selected, I need to dynamically move a combination of them into another arrayList.
Currently, I am achieving this using if/else statements. Is there a more dynamic way to accomplish this?
Here is a snippet of my code :
if (
this.selectedSources.some(x => x.Value === "B") &&
this.selectedSources.some(x => x.Value === "C")
) {
if (this.formulalist != undefined) {
//this.formulalist.length = 0;
this.formulalist = [];
}
this.formulalist.push({
Value: "B - C",
Name: "B - C",
IsVisible: true,
UpdateFlag: "A",
Market: "",
FormulaType: "diff",
FormulaSet1: "1",
FormulaSet2: "3",
checked: null
});
this.formulalist.push({
Value: "% B - C",
Name: "% B - C",
IsVisible: true,
UpdateFlag: "A",
Market: "",
FormulaType: "percent",
FormulaSet1: "1",
FormulaSet2: "3",
checked: null
});
else if (
this.selectedSources.some(x => x.Value === "B") &&
this.selectedSources.some(x => x.Value === "D")
) {
if (this.formulalist != undefined) {
//this.formulalist.length = 0;
this.formulalist = [];
}
this.formulalist.push({
Value: "B - D",
Name: "B - D",
IsVisible: true,
UpdateFlag: "A",
Market: "",
FormulaType: "percent",
FormulaSet1: "1",
FormulaSet2: "4",
checked: null
});
this.formulalist.push({
Value: "% B - D",
Name: "% B - D",
IsVisible: true,
UpdateFlag: "A",
Market: "",
FormulaType: "percent",
FormulaSet1: "1",
FormulaSet2: "4",
checked: null
});
............
............
If the user selects B and C, I need to push a combination of these items. For example: (B - C) and % B - C.
How can we achieve this dynamically without using hard-coded values, but based on the user selection in angular 4
The selectedSources array contains the list selected by the user.
If the user selects B, A, C, I need to push these items into another array list with combinations like A - C, % A - C, A - B, % A - B, B - C, % B - C