Reset the counter back to zero before it begins anew at 1

I am looking to create functionality that involves storing the number of read messages in an array. I want to then reset this counter based on the difference between the length of the message array and the current counter value.

this.badgeCount = this.messages.length //5

The this.messages.length value is obtained from the service for each individual message.

Whenever the matMenu is closed, I need to reset the counter to zero and mark all messages as read.

menuClosed(){
 this.badgeCount = 0;

 this.messages.forEach((message: Message){
  message.read = true;
 })

}

The issue I am facing is that the this.messages.length always reflects the actual length, let's say 5. As a result, when a new message arrives, causing the length to increase to 6, the badgeCount also resets to 6 instead of 0. Is there a way to store the read messages in an array and calculate the count based on the total length of messages?

Here is a working sample

Answer №1

To streamline the service, focus solely on counting unread messages as demonstrated in the code snippet below:

this.unreadCount = this.messages.filter((message: Message) {
    return !message.read;
}).length;

Answer №2

It seems like your question may be a bit unclear, but I'll do my best to provide an answer.

If you wish to store read messages in a separate array, you can simply declare the array and add messages to it using the push method.

menuClosed(){
 this.badgeCount = 0;
 var readMessages : Message[];

 this.messages.forEach((message: Message){
  message.read = true;
  readMessages.push(message);
 })

 //You can now access the length of readMessages
}

If you only need the count of read messages without storing them in a separate array...

menuClosed(){
 this.badgeCount = 0;

 this.messages.forEach((message: Message){
  message.read = true;
 })

 //Store the count of read messages in a class variable for future use
 this.readMessages = this.messages.length ;
}

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

Switch to the designated tab when clicked

I'm new to Vuejs and I am struggling to change the selected tab in the navigation bar when clicking on it. I tried using a function but I keep getting an error message in the console: vue.runtime.global.js:8392 Uncaught TypeError: _ctx.changeTab is ...

Vue.js - computed property not rendering in repeated list

It seems like the issue lies in the timing rather than being related to asynchronous operations. I'm currently iterating through an object and displaying a list of items. One of the values requires calculation using a method. While the direct values ...

Is there a method to determine the string name value of the defining property in TypeScript?

Currently, I am working on developing a GraphQL application and facing a challenge with my GQL type definitions. organization: { type: OTCompany, astNode: fieldDefinitionAST(OTCompany.name, 'organization', [authDirective()]), description: ...

Retrieving information from Express to Angular

Seeking assistance from the community. I've been diving into learning angular recently and encountered a challenge with displaying data from a server in a simple angular app. I have set up an express local-host server, and within my server folder, I ...

Error encountered during AJAX POST request: NETWORK_ERR code XMLHttpRequest Exception 101 was raised while using an Android device

Here is the ajax post code that I am using: $.ajax({ type: "POST", url: "http://sampleurl", data: { 'email':$('#email').val(), 'password':$('#password').val(), }, cache: false, ...

Steps to retrieve the latest value of a specific cell within the Material UI Data Grid

After updating the cell within the data grid, I encountered an issue where I could retrieve the ID and field using the prop selectedCellParams, but retrieving the modified value was proving to be challenging. In order to successfully execute the PUT reque ...

Can one designate something as deprecated in TypeScript?

Currently, I am in the process of creating typescript definitions for a JavaScript API that includes a deprecated method. The documentation mentions that the API is solely for compatibility purposes and has no effect: This API has no effect. It has been ...

Loading input datalist in Firefox postponed

My goal is to implement an input field for a username that allows users to select from a wide range of names/usernames. I want them to be able to enter a partial string from the name or username. Instead of loading the entire list initially, I aim to load ...

Bamboo causes alarm: terminating build process because of failed terminal initialization while constructing Angular application

I am currently working on an Angular app and have encountered an issue. When I use the command ng --aot build directly in the terminal, everything runs smoothly. However, when I try to run it through a bamboo script task, I receive the following error: pa ...

Using jQuery to align a div element to the top of the viewport

Is there a way to keep the #lightbox div positioned at the top of the viewport consistently? $(document).ready(function(){ $('.mehr').click(function() { $("body").css("overflow", "hidden"); $('#lightbox').css({'visibil ...

Uncertain as to the reason behind why the ng-repeat-start and ng-repeat-end are causing an

To begin, let's provide some background on what I aim to accomplish: <tr ng-repeat-start="eachParam in myArray"> <td rowspan=2>On site</td> <td rowspan="2" class = "success">{{eachParam.support}}</td> <td ...

Tips for utilizing a ForEach loop in JavaScript to create an object with dynamically provided keys and values

Looking to create a JavaScript object with the following structure, where the Car Make and Model Names are provided from other variables. { "Sedan":{ "Jaguar":[ "XF", "XJ" ], "AUDI":[ "A6", ...

Is a finished callback needed for .on('xxx') event?

On my dashboard page, I am currently retrieving the top 25 comments and displaying them using the following code: fba.orderByChild('when').limitToLast(25).on('child_added', function (d, c) { stuff }); However, the function is called f ...

Exploring Netbeans 7.3 for Enhanced JavaScript Autocomplete with Parameters

Ever since the upgrade to netbeans 7.3, I've encountered an issue where netbeans no longer automatically provides me with parameter names. For instance, When I see "methodA(param1, param2) xxx.js" in the autocomplete pop-up, clicking on it or pressi ...

Remove data from a database using Ajax in ASP.NET MVC without utilizing DataTables

I'm encountering a slight issue with my code. I am attempting to delete a row from the database without using DataTables in ASP.NET MVC, but it seems to be not working as expected. I have displayed all items from the database on a page within div elem ...

Developing a pop-up feature that triggers upon clicking for a miniature rich text editing

Looking to integrate the Tiny rich text editor into my code. Check out the TextEditor.js component below: import React from 'react'; import { Editor } from '@tinymce/tinymce-react'; class App extends React.Component { handleEditorCha ...

Delete the initial image from the opening list item using jQuery

Here is an example of some HTML code: <ul class="products"> <li> <a href="#" class="product-images"> <span class="featured-image"> <img src="img1.jpg"/> <img src="img ...

Executing multiple commands within a single child process in Node.js

Is it possible to create a new process in node.js and execute multiple terminal shell commands along with running node.js functions like fs.writeFileSync within the same context? For instance, I would like to perform the following tasks within a single pr ...

What is the best way to showcase a Firestore timestamp in a React application?

Struggling to showcase a Firestore timestamp in a React app. A Firestore document holds a field called createdAt. Attempting to present it within an output list (filtering out irrelevant details). componentDidMount() { this.setState({ loading: true ...

I am having trouble understanding the issue with this React component

I am currently working on developing a CRUD application using MySQL and Express. My current challenge involves redirecting the user when attempting to register with a username that already exists in the database. However, despite implementing a try/catch b ...