"Discover the method for calculating the length of a list where the 'read' attribute

My list looks like this:

let chats: Chats[] = [
{id: 1, name: "James Anderson",read=false},
{id: 2, name: "James Anderson",read=true},
{id: 3, name: "James Anderson",read=true},
{id: 4, name: "James Anderson",read=true},
]

On my cshtml page, I have been using {{chats.length}} to get the total count. However, I now need to retrieve the count where the read value is set as false.

Therefore, I expect the count to be 1 instead of 4.

Answer №1

To check the number of unread messages in an array, you can utilize the array.filter method along with length property like so:

let result = chats.filter(t => !t.read).length;

EXAMPLE

var chats = [
{id: 1, name: "James Anderson", read: false},
{id: 2, name: "James Anderson", read: true},
{id: 3, name: "James Anderson", read: true},
{id: 4, name: "James Anderson", read: true}
];

let result = chats.filter(t => !t.read).length;
console.log(result);

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

Enhancing Javascript performance with the power of the V8 engine and Typescript

I recently came across an informative article discussing V8 engine and javascript optimization. How JavaScript works: inside the V8 engine + 5 tips on how to write optimized code The article highlights some key recommendations: a. V8 engine utilizes hid ...

"Creating a Typescript function that guarantees a non-null and non-undefined return

Currently, I am working on developing a function that is designed to return a specific value. In the event that the returned value is null or undefined, the function should then default to a pre-determined value. function test<A, B>(input: A, fallba ...

Tips for embedding an object into an iframe source

I have successfully integrated a YouTube iframe into my HTML file. However, I would like the source URL to be dynamically generated based on data from the backend rather than manually inputting the URL as shown below. In the admin panel, there is an object ...

The distinction between "parameter?: type | undefined" and "parameter?: type"

Can someone help me grasp the distinction between two methods of defining an optional parameter in a function: parameter?: type and parameter?: type | undefined My understanding is that the first option is essentially a shortcut for parameter: type | und ...

Error: The code is unable to access the '0' property of an undefined variable, but it is functioning properly

I am working with two arrays in my code: bookingHistory: Booking[] = []; currentBookings: any[] = []; Both arrays are populated later in the code. The bookingHistory array consists of instances of Booking, while currentBookings contains arrays of Booking ...

Pause the execution at specific points within Typescript files by utilizing breakpoints when debugging with an attached debugger in VsCode

My current challenge involves setting up a debugger in VsCode with the attach mode on a Typescript codebase running in a Docker container. Despite successfully attaching the debugger in VsCode and hitting breakpoints, I consistently find myself landing on ...

The 'React' namespace does not contain the exported members 'ConsumerProps' or 'ProviderProps'

Is it possible to install this library in Visual Studio with React version 15.0.35? Are there any other libraries that are compatible with this specific React version? import * as React from 'react'; import { RouteComponentProps, NavLink } from ...

How to effectively utilize the Angular Material Button individually in a Stackblitz environment?

Most of the time, I rely on Angular Material when working on Stackblitz. Usually, I just import a comprehensive module and everything functions smoothly. However, I recently attempted to solely import the Angular Material Button Module and encountered thi ...

Exploring the use of Typescript to map a local array within a React card component

I have a set of Interfaces that look like this: interface INewsProps { newsType: string; data: INewsContentProps[]; } interface INewsContentProps { title: string; newsContent: string; } This is the array of news items I am working with: export co ...

Embedding an image by inserting the URL

I have been attempting to implement functionality in Typescript where an image preview appears after a user enters a URL, but I have only been successful in achieving this in JavaScript. My goal is to display a preview of the image and enable the user to u ...

Is it possible to incorporate a media query within a function?

When the button is clicked, my method is called. I utilize media queries to adjust the position to the right. method(){ $(".window").animate( { right: 420 // apply media query here },5000) } I am int ...

What is the best way to convert an array received from an @Output emitter into a formGroup with formControls in Angular reactive forms?

In the past, I have been able to initialize a form array of form controls when the array value is available during onInit. However, the situation is different this time. The array value will be received from the output of my child component only. parentco ...

Testing the ngOninit Lifecycle Hook in Angular 2 Components

There is a listing component with the code provided below: ///<reference path="../../node_modules/angular2/typings/browser.d.ts"/> import { Component, OnInit } from 'angular2/core'; import { ROUTER_DIRECTIVES } from 'angular2/router&a ...

Creating a unique theme export from a custom UI library with Material-UI

Currently, I am in the process of developing a unique UI library at my workplace which utilizes Material-UI. This UI library features a custom theme where I have integrated custom company colors into the palette object. While these custom colors work perfe ...

Embed an injection token into a library service, which is then utilized within an Angular application in a transitive manner

After developing an angular library named library1, I have created a service called APIService within it. The APIService requires a constructor argument (URL: string), which is injected using an Injection token defined as, const URL_TOKEN = new InjectionT ...

Tips for sorting data based on duplicate dates within a single array element

Here is the existing Object Array structure: [ { "date":"12-09-2019 12:00 PM", "id":"1", "name":"hello1" }, { "date":"12-09-2019 03:00 PM", "id":"2", "name":"hello2" }, { "date":"12- ...

Discover the seamless method for transferring information between routed components in Angular

My situation involves a routed component that looks like this: https://i.sstatic.net/ICHz4.png In Component A, I have an object obtained through an API. Currently, I have a button that redirects me to Component B. There is no direct parent-child relatio ...

Strategies for dynamically appending data to `from([urls])` using RxJS

const dataResults = []; from(generateMoreUrls()) .pipe( mergeMap( url => fetch(url), 4) ).subscribe(dataResult => { dataResults.push(dataResult); }); function generateMoreUrls() { let additionalUrls = []; for(let i = 101; i < ...

What are the steps to display Firestore data on the server instead of the client side?

My objective is to display data on the server side. Expected outcome: the server should render the data. Actual outcome: the data is being rendered by the client instead of the server. The workflow follows this pattern Component -> call Use Case -> ...

After incorporating all the suggested guidelines from the eslint documentation into my .eslintrc.js file, I encountered numerous instances of the error message 'Rule definition was not found'

In my .eslintrc.js file, I have configured the following settings: module.exports = { parser: '@typescript-eslint/parser', parserOptions: { project: 'tsconfig.json', sourceType: 'module' }, plug ...