In this snippet of JSON code, the key "pants" is nested under the "children" key for MALE
when gender
is selected as male
. However, if the selected gender
is female
, then "pants" becomes a children key of FEMALE
.
var data = {
"gender": "male",
"myFilter": {
"MALE": {
"pants": ["33", "34"]
},
"FEMALE": {}
}
}
console.log(myFilter.data[gender.value].pants[0]);
I am looking to modify this code to make it more efficient. Instead of using the selected gender directly as a key, I want to utilize it as a variable key. So far, my attempts have been unsuccessful.
data
contains the entire JSON payload.