I got the following objects. I want to filter them in javascript, and get alle the objects with the area name of "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"
}
}
]
Currently my javascript looks as follows:
fetch(baseURL + "/employees")
.then(response => response.json())
.then(result => {
let vibeEmployees = result.filter(employee => employee.includes('Vibestuen'));
console.log(vibeEmployees)
})
Im getting the following error:
Uncaught (in promise) TypeError: employee.includes is not a function
at showEmployeeVibe.js:4
at Array.filter (<anonymous>)
at showEmployeeVibe.js:4
How do i filter these objects?
>Solution :
includes check if parameter is contained on Array. You filter should look like this
result.filter(employee => employee.area && employee.area.name === 'Vibestuen')