I'm creating an option selector, were you're able to select options for a certain machine. See image with a design of the objects:
Object:
Code:
export class Machine{
Id: string;
MachineGroup: MachineGroup;
Price: number;
Description: string;
Image: Image;
Weight: number;
Power: number;
Speed: number;
Name: string;
BomId: number;
Options: Option[];
}
export class Option{
Id: number;
Price: number;
OptionGroup: OptionGroup;
Description: string;
}
export class OptionGroup{
Id: number;
Image: Image;
Name: string;
}
The options need to be sorted under their OptionGroup, is there some kind of LINQ I can use within Angular?
I mean I can make a function within the component which loops though the list of options and add those objects to a new list to show. But this gave an undefined error, while the component isn't rendered before the Machine is set. The console.log printed the object like it supposed to be. The error is literally: "this is undefined", so I expect the OptionGroup array.
Rendered if machine is set:
<div *ngIf="machine">
<app-option-selector [inputMachine]="machine"></app-option-selector>
</div>
Component with function:
export class OptionSelectorComponent implements OnInit {
@Input() inputMachine: Machine;
options: Option[];
optionGroups: OptionGroup[];
constructor() { }
ngOnInit() {
this.options = this.inputMachine.Options;
this.setOptionGroups();
}
setOptionGroups(){
this.options.forEach(function(e){
console.log(e.OptionGroup);
this.optionGroups.push(new OptionGroup())
})
}
}
So is there an Angular way to create such sorting? Not prone to errors, like I tried to do?

this.optionGroups, but you never initializedthis.optionGroups, so it's undefined.optionGroups: OptionGroup[] = [];is initialization right? But it gives the same error.forEachloop, so you can’t usethis. You should use an arrow function instead insideforEach.