Is it possible to dynamically pass a context from the server to the client so that the client can retrieve a value from an object more efficiently?
For example, the server sends an object with a key-value pair like this:
"booking__validating_carrier__iata": "Cia"
.
The key
booking__validating_carrier__iata
can be translated into an array ['booking', 'validating_carrier', 'iata']
. The goal is to access the value order['booking']['validating_carrier']['iata']
or order.booking.validating_carrier.iata
I'm trying to figure out a way to access the object without hardcoding the indexing levels. One approach I came up with is:
if(arr.length === 1) {
order[arr[0]]
} else if(arr.length === 2) {
order[arr[0]][arr[1]]
} else if(arr.length === 3) {
order[arr[0]][arr[1]][arr[2]]
} else if(arr.length === 4) {
order[arr[0]][arr[1]][arr[2]]
} else if(arr.length === 5) {
order[arr[0]][arr[1]][arr[2]][arr[3]]
}
Is there a more efficient way to loop through the array length to access nested properties of the object?
Thank you!