Tips for eliminating objects with a sessionID:null value from the nested array within an array of objects using JavaScript

[
    {
        "_id": "5ecda7f5310bee6f4b845add",
        "firstname": "john",
        "lastname": "smith",
        "sessions": [
            {
                "_id": "5efc724e46d8330a449e7111",
                "sessionID": null
            },
            {
                "_id": "5efffea946d8330a449e7133",
                "sessionID": null
            }
        ]
    },
    {
        "_id": "5ecef2db7aa45475d94291ad",
        "firstname": "lisa",
        "lastname": "jones",
        "sessions": [
            {
                "_id": "5efc735746d8330a449e7115",
                "sessionID": null
            }
        ]
    },
    {
        "_id": "5ebde0479abcf021acdeb661",
        "firstname": "Emily",
        "lastname": "Brown",
        "sessions": [
            {
                "_id": "5efd8c59489cee59cc685ed4",
                "sessionID": null
            },
            {
                "_id": "5efe05509bb1fc4338fd547a",
                "sessionID": null
            }
        ]
    },
    {
        "_id": "5eddc95537060af47cd90790",
        "sessions": [],
        "firstname": "Michael",
        "lastname": "Johnson"
    }
]

Answer №1

You should iterate through your array and remove any sessions that have a null value

let startingArray = [
    {
        "_id": "5edfb4e587a1873120735dcf",
        "firstname": "abc",
        "lastname": "abc",
        "sessions": [
            {
                "_id": "5efc68d146d8330a449e7108",
                "sessionID": null
            },
            {
                "_id": "5efc68e646d8330a449e710a",
                "sessionID": null
            }
        ]
    },
    .... the remaining parts of your original object
]

let newArray = startingArray.map(item => {
    let sessions = item.sessions.filter(session => {
        return session.sessionID !== null
    })

    return {
        ...item,
        sessions
    }
})

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

Error: The specified object does not contain the 'tableRow' method

I am currently working on a contacts book project and I need a table to update as the user inputs data. However, I keep encountering an error related to 'tableRow'. I have tried changing function names and other solutions but haven't been ab ...

Creating a group object based on ID using react-native and JavaScript - a step-by-step guide

I have an array of objects that I need to reorganize so that each object with the same guid is grouped together. For instance, given this array; [ {"guid":"3a03a0a3-ddad-4607-9464-9d139d9989bf","comment":"text&quo ...

A gojs swim lane diagram requires a date axis to provide accurate time representation

I am currently working on a web application that requires a vertical swim lane activity diagram with a customized date axis displayed alongside it. The nodes in the diagram should be positioned based on their date attribute, and users should have the abili ...

Which specific HTML element triggers the initiation of ajax in Jquery?

I have a situation where various HTML elements trigger ajax requests when clicked. My goal is to determine which specific element was clicked inside the ajaxComplete event. Although I attempted to use event.target, it only returns the entire document inst ...

Retrieving information from an openDatabase using AngularJS

Encountering an issue while trying to retrieve data from openDatabase and display it in a listview. Following advice from a thread, I added $scope.$apply(); after $scope.items = $data;, but this resulted in an error: [$rootScope:inprog] $apply already in p ...

Displaying Photo on Webpage using Bearer Authorization Token in JavaScript

In my JavaScript code, I am currently loading an image using the following method: var img = new Image(); img.onload = function () { .. }; img.src = src; However, I have recently realized that I need to secure the images on my server side with OAuth 2, j ...

What is the proper method for implementing a HAVING clause in a MongoDB GROUP BY statement?

In SQL, what is the equivalent query to find duplicates? SELECT userId, name FROM col GROUP BY userId, name HAVING COUNT(*)>1 Instead of using SQL, I ran a similar query in MongoDB: res = db.col.group({key:{userId:true,name:true}, ...

Utilizing the for loop to access a Json array

How do I iterate through an array with the given structure using a for loop? My Array: { "cod": "200", "message": 0.0027, "city": { "id": 2264456, "name": "Portimao", ...

Retrieve the HTML content of all children except for a specific child element in jQuery

Is there a way to utilize jQuery/Javascript for selecting the HTML content of the two <p> elements in the initial <div class="description? I'm open to using Regex as well. This specific jQuery selection is being executed within Node.js on a c ...

Combining PHP with JSON encoding to power a dynamic and interactive data table

I am utilizing a jQuery plugin known as DataTables, which necessitates a JSON string to be returned for table rendering. At present, the outputted JSON appears like this (check out the aaData array): {"sEcho":0,"aaData":[[{"ID":"1","idPatient":"122342"," ...

Instructions for serializing a Jackson field as a JSON string rather than an object

Working with Amazon SNS push notifications involves sending a specific payload format: { "default": "This is the default message required when publishing a message to a topic. It will be used if no message is specified for a platform.&quo ...

What's the better approach for creating Maps and Sets in ES6: relying on a compiler or leveraging syntactic sugar?

It has always baffled me why we are unable to write ES6 Maps in the following way: new Map({ ['ArrayAsKey']: {foo:bar} }) Is there a workaround for this limitation? Or perhaps a technique that enhances the experience of utilizing these mode ...

Displaying the current time and total time of a custom video player using Javascript

Currently, I'm in the process of creating an html5 video player and have incorporated javascript to update the current time as a fraction of the total time. The script I've written so far is as follows: function updateTime() { var curTime = ...

Error in 'Unity3D': Array Index Out Of Bounds Exception occurred while attempting to modify sprite upon collision

Hey there, fellow Developers! I know this question has been asked before, but none of the answers provided a solution to my specific problem. Just to give you some context, I am new to unity and currently working on creating a brick breaker game. The issue ...

Exiting a void method in JavaScript/Typescript using the return or break statement

I find myself dealing with a complex method in Typescript that contains multiple if else if else constructs. It's a void method, and I'm wondering how I can exit the method based on a specific if condition without having to execute the remaining ...

Webpack returns an undefined error when attempting to add a JavaScript library

I am a newcomer to webpack and I am attempting to incorporate skrollr.js into my webpack setup so that I can use it as needed. However, I am unsure of the correct approach for this. After some research, I have found that I can either use an alias or export ...

How to extract a variable value from a different HTML page using jQuery

I have an HTML page named "page1.html" with the following content: <div> <div id="content"> content </div> <script type="text/javascript"> var x=5; var y=2; </script> <div id="ot ...

Error: MongoDB connection rejected on port 3001 while using ReactJS

I have encountered an issue with my MongoDB connection being refused at port 3001. It was working fine previously but suddenly stopped. My database is hosted on MongoDB atlas and I am using MongoDB compass to connect with the link in my code. Despite fol ...

Unraveling nested arrays in Swift using dictionary

I am encountering an issue with the error message: "Expected to decode Array<Any> but found a dictionary instead." when attempting to decode JSON data in the form of a Dictionary? The JSON data structure is as follows: { "status&quo ...

Unable to generate an array of SCNNodes due to an error

Struggling to create an array of nodes for a series of images in Scene Kit, as my experience in this area is limited. I am attempting to achieve the following: func addNumbers() { let v1 = drand48() let v2 = drand48() let v3 = drand48() ...