Working with Angular to iterate through an array while applying a specific

I receive data from the server and use it to display notifications to users.

My goal is to send notifications to everyone except the sender, but I'm not sure how to loop through the user data to achieve this.

Code

I have included comments for clarity in the code snippet below

// Notify group
this.socket.fromEvent('message').subscribe(async (message: any) => {
    console.log('group message: ', message); // see screenshot below

    // Loop through group users and exclude the sender, then send notification to everyone else
    // Accessing group users - tested `message.msg.message.group.users`

    // Need help with looping through here

    // Condition to avoid sending notification to the sender
    // Sender's username - tested `message.msg.message.user.username`
    // if (message.msg.message.user.username ............??? ) {
    const notifs = LocalNotifications.schedule({
        notifications: [
        {
            title: message.msg.message.user.username,
            body: message.msg.message.note,
            id: Math.floor(Math.random() * 10),
            schedule: { at: new Date(Date.now() + 1000 * 2) },
            sound: 'beep.wav',
            attachments: null,
            actionTypeId: 'OPEN_CHAT',
            extra: null
        }
        ]
    });
    console.log('scheduled notifications', notifs);
    // }
});
// Notify group

Screenshot

https://i.sstatic.net/wsIO1.png

Any assistance on accomplishing this task would be greatly appreciated. Thank you!

Update

To clarify:

LOGIC

  1. Each user has a unique username
  2. The user object is the sender of the notification
  3. Group users are members of the group that the notification is sent to (including the sender)
  4. The purpose of looping through group users is to exclude the sender in order to prevent them from receiving the notification Thus, everyone in the group will receive the notification except the sender

Hope this explanation clarifies things a bit.

Answer №1

To iterate through items in an array, you can utilize the for-of syntax:

for(const item of items) {     
  if(item !== target) { ... }
}

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

In JavaScript, the input box is set to automatically capitalize the first letter, but users have the ability

How can I automatically capitalize the first letter of a user's name input, but allow for overrides like in the case of names such as "de Salis"? I've read on Stack Overflow that CSS alone cannot achieve this with text-transform:capitalize;, so ...

Managing individual promises within an array efficiently

I am facing an issue with my function that calls web services on my server and returns an array of promises. The problem is that if one of the calls fails, the entire process is marked as a failure. For example, out of 5 calls, 1 might fail, causing the w ...

Fundamentals of object property in jQuery and Javascript

Currently working on creating a new property named startPosition and setting the top property to equal the button's current top value in CSS. Below is the jQuery code snippet: var morphObject = { button: $('button.morphButton'), c ...

Countdown clock facing a date malfunction in JavaScript

After spending some time writing code to create a countdown clock for my website, I've run into an unexpected issue. Here is the HTML snippet: <div id="deadline_Container"> <div id="clockdiv"> <span class="deadline_Text">Time rem ...

Updating a subscribed observable does not occur when pushing or nexting a value from an observable subject

Help needed! I've created a simple example that should be working, but unfortunately it's not :( My onClick() function doesn't seem to trigger the console.log as expected. Can someone help me figure out what I'm doing wrong? @Component ...

404 error received from Angular 2 API call despite successful testing of service

I've been attempting to make a web service call from my Angular 2 application. private baseUrl: string = 'http://localhost:3000/api'; getPatterns(): Observable<Pattern[]> { const patterns$ = this.http .get(`${this.baseUrl ...

How can ngFor be used to iterate through a nested object that contains a list requiring indexing?

I attempted to display it in the following manner <tr *ngFor="let item of mf.data; let i = index;" [attr.data-index]="i"> <td>{{item.grouporganizationunit.organizationunitlist[i].organizationunits.name}}</td> However, an error is being ...

What are some techniques to ensure null checking is enforced for objects that are nested within multiple layers

Currently, I am leveraging redux sagas to fetch data asynchronously from various endpoints using a unified interface: export interface ResponseInfo { data?: any; status: number; headers?: any; subCode?: string; } To ensure that null check ...

What are the steps to globalize the ng-bootstrap datepicker?

For my current project, I am utilizing the ng-bootstrap datePicker component. The demo for my simple datePicker widget can be found here. However, I am now seeking to internationalize it by setting it to use the Russian language. Any assistance with this ...

Scope Function Challenges in Vue.js Method

Having a SetInterval embedded within a Promise in Axios is causing an error when trying to run a function inside the SetInterval block. The specific error encountered is: methods: { getJson() { axios.post(url, FormObject, config) . ...

How can I show a limited number of columns in a mat-table in Angular 6 depending on a specific condition?

Currently, I am facing an issue with my mat table as it contains several columns. In a specific scenario, I need to hide two of these columns. Typically, for a regular table, we would use a simple ngIf condition to achieve this. However, in the case of thi ...

Optimal method for effectively incorporating Angular routerLink URLs within an HTML string

In the angular app I'm working on, I have a notification service that is usually called like this: this.notificationsService.showError('My Title', 'My Message...'); I want to be able to include app links in the messages as well, ...

Guide to triggering an API call upon changing the value in a Vue Multiselect component

Is there a way to trigger an API call when the value changes in a Vue Multiselect component? I want to update the value in the multiselect, make an API call, and display the result in the console. Below is my code snippet. <template> <div> ...

Please provide a validation error message when attempting to select a dropdown list while the checkbox is unchecked

There are 7 drop down menus in a table row. When trying to select a value from the drop downs with the checkbox unchecked, a validation message should be displayed saying "Please enable the checkbox". If the checkbox is checked, all the drop down menus sh ...

Verify that the lower limit is not higher than the upper limit set in an Angular application

In my Angular application, I have two input fields for minimum and maximum values. I want to display an error message if the user enters a minimum value greater than the maximum value provided, or the default maximum value if none is provided. The form is ...

Steps to automatically hide dropdowns when a menu item is selected

I am working on a dropdown menu using NUXT JS and Tailwind CSS. However, I have encountered an issue with nuxt-js - since it does not change pages using SSR, the dropdown does not close when navigating to a new page. How can I make the dropdown automatical ...

Unable to locate the node module within the Rails stimulus controller

I'm facing an issue with integrating my own npm package into a Rails application that I have developed. It's unclear whether the problem lies in the creation of the node package or within the rails app itself. The package in question is fairly ...

Accessing attributes of a parent class object from within a child object

Imagine having four tabs within an Angular component, each with its own set of criteria for being displayed. Here's a high-level overview of the scenario. export class DisplayTabs { foo: true; bar: false; tabs: { 'A': { order: 1, g ...

Utilize the power of generics with Angular's service providers

Is it possible to make the membervar of class Parent generic instead of type any, while still retaining the ability to switch provider classes without having to update all components that rely on class Parent? For example, if class ChildB implements a diff ...

Effectively managing user access by authorizing levels and securing routes

Below is the code snippet for a protected route where the authentication status is managed by Redux. If there is no token saved in local storage, the isAuthenticated state is set to false. This code snippet is for protecting routes: import PropTypes from & ...