I have a class Rule as it follows:
export class Rule {
public text: string
public severity: number
constructor(
text: string,
severity: number,
) {
this.text = text
this.severity = severity
}
}
How can I generate the following object incidenceRules from an array of Rule?
export interface IncidenceRuleObj {
ruleObj: Rule // The whole object
text: string // Rule.text should go here
}
export interface IncidenceRules {
[severity: number]: IncidenceRuleObj
}
export const incidenceRules = (rules: Rule[]): IncidenceRules => {
return rules.map(rule => {
// What should go here in order to return an IncidenceRules type object?
})
}
As an example, this should return the same object structure as in this question.
This returned object from incidenceRules() should not be "hardcoded", therefore the iteration over rules objects is needed.