I have a list of objects that I want to filter in JavaScript to only display the ones with the area name "Vibestuen".
[
{
"id": 292,
"name": "Hans",
"image": "as",
"calculatedVacation": 1.0,
"area": {
"name": "Ikke tilknyttet en stue"
}
},
{
"id": 295,
"name": "xx",
"image": "zz",
"calculatedVacation": 2.0,
"area": {
"name": "Vibestuen"
}
},
{
"id": 296,
"name": "xx",
"image": "abccc.png",
"calculatedVacation": 2.0,
"area": {
"name": "Andestuen"
}
},
{
"id": 298,
"name": "bunun",
"image": "zz",
"calculatedVacation": 2.0,
"area": null
},
{
"id": 299,
"name": "lort",
"image": "kol",
"calculatedVacation": 2.0,
"area": {
"name": "Vibestuen"
}
}
]
This is my current JavaScript code:
fetch(baseURL + "/employees")
.then(response => response.json())
.then(result => {
let vibeEmployees = result.filter(employee => employee.area && employee.area.name === 'Vibestuen');
console.log(vibeEmployees)
})
The error message I'm receiving is:
Uncaught (in promise) TypeError: Cannot read property 'includes' of undefined
at showEmployeeVibe.js:4
at Array.filter (<anonymous>)
at showEmployeeVibe.js:4
How can I properly filter these objects?