I'm trying to minimize duplicate code like:
let name = "";
try {
name = data[" "][0][" "][0][" "][0][" "][1][" "][0][" "][1]["C"];
} catch (error) {
if (error instanceof TypeError) {
console.log("name TypeError");
}
}
I thought about creating a function that I can pass it the JSON parameter (data
variable) with the path (i.e. data[" "][0][" "][0][" "][0][" "][1][" "][0][" "][1]["C"]
) so the error handling will be in the function and I will not have to duplicate the try catch
every time (I extract data from that JSON a lot).
But I cannot find a proper way to do it. Would appreciate any help!