I would like to know as to how can we skip Undefined error being throw if a key is missing.
[
{
"id": "foo1",
"name": "Test1",
"description": "random test1",
"languages": [
"en"
],
"subThemes": [
{
"id": "1",
"name": "Bike"
},
{
"id": "2",
"name": "Testy",
"description": "Testing this thing1"
}
]
},
{
"id": "foo2",
"name": "Test",
"description": "random test2",
"languages": [
"en"
],
"subThemes": [
{
"id": "3",
"name": "Bike2"
},
{
"id": "4",
"name": "Testy2",
"description": "Testing this thing2"
}
]
},
{
"id": "foo3",
"name": "Test3",
"description": "random test3",
"languages": [
"en"
]
}
]
Let say I have an array object like this above, If I do a map over it, it is giving me an error because I don't have a subThemes key in the 3rd object.
const children = theme.subThemes
.map(subTheme => {
const { subThemes = [] } = aggregations;
...rest of the code}
TypeError: Cannot read property 'map' of undefined
How do I bypass the undefined error? Thanks in advance.
theme.subThemes?.map(..)