Error: The employee function does not support includes method

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?

Answer №1

contains a check to see if the parameter exists in an Array. Your filtering criteria should resemble the following:

result.filter(employee => employee.area && employee.area.name === 'Vibestuen')

Answer №2

Here is one way to accomplish this task:

const vibeEmployee = result.filter(employee => employee.area && employee.area.name === 'Vibestuen')

Answer №3

Looking to update the filtering condition within a callback function. Some records have null values for the area property in the employee object.

For example, you can modify it like this:

let vibeEmployees = data.filter( (employee) => employee.area && employee.area.name.includes("Vibestuen") );

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is there a way to create a hyperlink in a JSON file that links to text within the same JSON using AngularJS?

The JSON structure I am working with is shown below: [ { "_id": "0", "_rev": "1", "url": "fb.com", "ownership": { "line": "social" }, "id": 13, "hierarchies": [ { "level": 30, "level40_desc": "", ...

Encountering an undefined response in API call using Fetch with Express Nuxt

I've hit a roadblock with a task involving Express, API, and Fetch. I'm utilizing Nuxt along with Shopify API endpoints to retrieve data such as orders. Below is my express API Endpoint which should return an array of objects (orders). const bod ...

Deleting a string by utilizing the Ternary operator in JavaScript

How do I remove the [object Object] from my output shown in the console? I'm looking to print the output without the [Object Object]. Is it possible to achieve this using a ternary operation? data:[object Object] text:Data is supplied by Government ...

NodeJs backend encounters undefined object due to FormData format request sent from Angular frontend

Never encountered this issue before despite having used this method for a long time. (Angular Frontend) const myFormData = new FormData(); myFormData.append("ok", "true"); this.http.put(my_Express_backend_url, myFormData); (Express ...

Utilizing Powershell to eliminate characters from a string

After pulling a list of IPs from a JSON file, I've been using the code snippet below: $Request = 'https://url-to-json.com/file.json' $AWSIPs = Invoke-WebRequest $Request | ConvertFrom-Json | Select-Object prefix -ExpandProperty prefixes -Ex ...

Integrating dynamically generated fields into Django JSON serialization

In my Django app, I have defined an API like this: Here is the JSON data being sent from my api.py: class studentList(APIView): def get(self, request, pk, format=None): student_detail = Student.objects.filter(student_id = pk) serializ ...

Experience the magic of a customized cursor that disappears with a simple mouse movement in your website,

I have been experimenting with designing a custom cursor for my website. After finding a design I liked, I made some adjustments to suit my needs. However, an issue I encountered is that when I scroll, the custom cursor also moves away from its original po ...

Implementing a div element within an autosuggest feature

i am currently integrating the bsn autosuggest into my project could someone please guide me on how to insert a div in the result so that it appears like this <div style="left: 347px; top: 1024px; width: 400px;" class="autosuggest" id="as_testinput_x ...

The click event in jQuery/JavaScript is not functioning

Although it should be as simple as breathing, I just can't seem to spot the mistake in my code... I attempted to add a click event to a div with a unique ID, but unfortunately, it's not working at all. This issue persists with both jQuery and Ja ...

Display the div only when the time variable reaches zero

I want to display a div when the difference between the time imported from the database and the current time is 0. How can I achieve this? Here is the code snippet: while ($row = mysqli_fetch_array($result)) { echo "<div class='alert' id= ...

Display webpage when a user picks a font from a dropdown menu using jQuery

I am looking to create a code that will display a list of fonts for the user to choose from. Once a font is selected, the content on the page will be displayed using that particular font face. Unfortunately, I'm not sure how to achieve this yet :( ...

Converting an array to an object with values in JavaScript

Can someone help me with converting an array to an object in JavaScript? I want to set each value to "true". The initial array is: ['lastName', 'firstName', 'email'] I need it transformed to: {lastName: true, firstName: tr ...

Exploring methods for monitoring page transitions in Next.js

Looking to repurpose a menu I created in react using react-router-dom for use in nextjs. My objective is to update the menu state to 'false' and change the menuName to 'menu' upon clicking on a link within the menu. Implemented a useEf ...

What is the best way to use jQuery to add items to my unordered list based on user

Here is the HTML code I have created: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name=& ...

Ways to effectively pair a radio button with a dropdown menu

function radioCheck() { document.getElementById("last").checked = "checked"; } <label> <input type="radio" name="Ppub" value="" checked="checked">All Dates </label> <br> <label> <input type="radio" id="last" name="Ppu ...

A guide on implementing an id field into an object using JavaScript

Is it possible to assign a distinct identifier to every element in the JSON array with JavaScript? [ { name: 'Yosi'}, { name: 'Ben' }, { name: 'Dan' }, { name: 'Erez'} ] Desired outcome: [ {id: 1, name: 'y ...

What are the steps for rebuilding an array of data?

The following data is available: { "data": [ { "a": 11, "b": 12, "c": 13 }, { "a": 21, "b": 22, "c": 23 }, { "a" ...

Exclude the map entry from displaying in a JSON document

I am currently working with the following endpoint: @RestController @RequestMapping(value = "/info") public class InfoEndPoint { .... @Autowired public InfoEndPoint(....) { .... } @RequestMapping(method = RequestMethod.GET) ...

Retrieving a specific value from a JSON object using JavaScript

Forgive me for what seems like a simple question, but I lack programming skills. I am attempting to extract a specific value from an API response. The JSON I receive contains multiple values, but I only require one particular piece of information. Here is ...

I am unable to submit the form to the server

I'm experiencing an issue with my login form. I've tried submitting it using jQuery, but it's not working as expected. I've searched online for solutions, but they all seem too complex. Here is the code I'm working with: <form ...