I have a json which I want to reference based on a variable:
matrix: {
nog: {
moves: number[][];
scale: number;
};
eyes: {
moves: number[][];
scale: number;
};
...
I have a variable art
where art may be ‘nog’ or ‘eyes’ or …
I would like to be able to extract data from the json with something like matrix.[art]
How might I do this (I could of course use a switch function, but looking for some thing more elegant that can scale)?
2
Answers
You can just make use of
matrix[art]
pattern. E.g. below js code just works:You should convert JSON to an object like this: