How can I use a simple JavaScript lookup table (which is the mapping itself) inside map function?
I.e how can i get rid of this "code" field (borrowed from here), and use only the lookup table inside map method?
const Resistors = {
"black": 0, "brown": 1, "red": 2,
"orange": 3, "yellow": 4, "green": 5,
"blue": 6, "violet": 7, "grey": 8,
"white": 9,
// Why not Resistors[color]
"code" : (color: string) => {
function valueOf<T, K extends keyof T>(obj: T, key: K) {
return obj[key];
}
return valueOf(Resistors, color as any);
}
}
class ResistorColor {
private colors: string[];
constructor(colors: string[]) { this.colors = colors; }
value = () => {
return Number.parseInt(
this.colors
.map(Resistors.code) // How can i get rid of .code here?
.join("")
)
}
}