const result = {
"MEN": [],
"WOMEN": [],
"KIDS TEMP": [
"BCDEFHJJJJJJ34EEE234",
"ABCDEFGHIJKL12345678"
]
}
const result = {
"MEN": [],
"WOMEN": [],
"CHILDREN TEMP": [
"BCDEFHJJJJJJ34EEE234",
"ABCDEFGHIJKL12345678"
]
}
I have an object result
like above. Sometimes, it may have KIDS TEMP
property and sometimes CHILDREN TEMP
. Since this property has spaces, I cannot use dot operator to fetch these properties.
I want to create a flag if either of KIDS TEMP
or CHILDREN TEMP
exist.
I tried below:-
const part = 'KIDS TEMP' || 'CHILDREN TEMP';
let check;
if (result) {
check = !!(result?.MEN.length !== 0 || result[part].length !== 0);
}
But the above code breaks when I receive CHILDREN TEMP
saying that it cannot read length of undefined.
How do I fix this?