What is the optimal location for storing a chosen collection of items?

There are multiple lists of elements available:

let list1 = [1,2,3,4]; 
let list2 = [1,2,3,4]; 
let list3 = [1,2,3,4]; 

Each list is being iterated through in a loop:

<div *ngFor="let el of list1" (click)="set(el)">{{el}</div>
<div *ngFor="let el of list2" (click)="set(el)">{{el}</div>
<div *ngFor="let el of list3" (click)="set(el)">{{el}</div>

Upon clicking an element within the <div>, the selected value needs to be stored like so:

selectedFilters = {list1: [1], list2: [2,3], list3: [4]}

Is there a better way to achieve this rather than creating separate classes for each list?

Additionally, the desired output includes converting all selected elements into a JSON format.

Answer №1

Consider adding an additional parameter to your set function, such as "source," to track the origin of the data.

Suggestion: Avoid naming functions with generic names like set, instead consider using a more descriptive name like addSelectedElem.

mySelectedElems: {
    list1: [];
    list2: [];
    list3: [];
}; // You can predefine the lists if you know their names

addSelectedElem(elem, source) {
    mySelectedElems[source] = mySelectedElems[source] || []
    mySelectedElems[source].push(elem)
}

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

Exploring the functionality of the Angular 7 date pipe in a more dynamic approach by incorporating it within a template literal using backticks, specifically

I have a value called changes.lastUpdatedTime.currentValue which is set to 1540460704884. I am looking to format this value using a pipe for date formatting. For example, I want to achieve something like this: {{lastUpdatedTime | date:'short'}} ...

It is possible that req.user may be undefined, especially when using express and passport.js

I am facing an issue with my Node.js TypeScript authentication system that utilizes passport. The problem arises when I attempt to access req.user in a specific route, resulting in the error message: Object is possibly 'undefined'. This behavio ...

What steps should I take to resolve the No Session error during the authentication process in a Laravel 9/Angular 13 SPA?

My Angular 13 SPA is successfully authenticating to Laravel 9 (Sanctum), as I am able to obtain the CSRF cookie and complete the authentication step. However, when I try to access other protected API endpoints, I am encountering a 'No Session' er ...

An error has occurred with mocha and ts-node unable to locate the local .d.ts file

This is the structure of my project: |_typetests | |_type.test.ts | | myproj.d.ts tsconfig.json Here is how my tsconfig.json file is configured: { "compilerOptions": { "module": "commonjs", "moduleResolution": "node", "lib": ...

When attempting to access the property 'originalname' of an undefined nodejs Mongoose object, an error is triggered

I am attempting to save images using mongoose, express, and multer. However, I keep encountering the following error when testing with Postman: TypeError: Cannot read property 'originalname' of undefined var express=require("express") var ro ...

What is the best way to determine the type of `rootReducer`?

My project is set up with a combination of React, Redux, Immutable.js, and TypeScript. As I worked on implementing it, I made an effort to declare types wherever possible which led me to discover an interesting issue. A code example illustrating the proble ...

bento-ng-d3 is missing from the npm registry, along with other bento packages, including Angular

I have encountered a dependency in my Angular project's package.json file that includes the following: "@bento/bento-ng":"8.4.1", "@bento/bento-ng-d3":8.4.1, "@bento/bento-ng-datamap":8.4.1, "@bento/bento-n ...

Utilize the super type as a generic parameter in TypeScript for stronger assertions

The Query Within TypeScript, utilizing the keyword extends allows for asserting a generic parameter as a 'subtype' of another type. For example: // class Base {} // class Child extends Base {} // edited: class Base { a = 1 } class Child extends ...

Difficulty with AMCharts Map displaying in production environment

I have successfully created an Angular application using AMCharts Map. During development mode (ng serve), the map renders correctly. Even when building the app without production mode (using the command ng build), the map displays as expected. However, ...

Angular chat integration

In my application, I have a parent component called "chat" with two child components - "sidebar" (which displays the user list) and "conversation detail" (which shows the chat with each user). The functionality I am aiming for is that when a user is clicke ...

Prevent automatic suggestions from appearing in Ionic app on Android

Our Ionic v4 (Angular, Cordova) app features form input fields (<ion-input>) like the example below: <ion-input formControlName="firstName" type="text" inputmode="text" autocapitalize="sentences"></ion-input> When running the Android ap ...

Discover the location of the class definition in VSCode with this helpful clue

Here is the code snippet I am working with: const Gatherer = require('../gatherer'); class MetaRobots extends Gatherer { /** * @param {{driver: !Driver}} options Run options … } module.exports = MetaRobots; When using VSCode, Driver ...

What is the best way to insert CSS code into a custom Vue directive file?

I need a solution that applies a gradient background to the parent div in case an image fails to load. I've attempted to create a directive for this purpose: export default { bind(el: any, binding: any) { try { ..... ...

Having trouble getting Angular2 tutorial animations to work in IE11?

After successfully setting up a local development environment using the Angular2 tutorial with the quick start seed [1], I decided to experiment with animations. Following a modified version of an example from [2], my app.component.ts now toggles backgroun ...

Eliminate information from Firestore with Angular

https://i.sstatic.net/MFKHB.png I encountered an issue while attempting to delete data from Firestore. Fetching and adding data is functioning correctly, but when attempting to delete data, it does not get removed and no error is displayed on the console. ...

Eliminate the underscore from mat-select in (@angular/material 15.0.3)

Is there a way to remove the underline from mat-select? <mat-form-field style="margin: 2em 2em 2em 2em" appearance="fill" > <mat-label>Choose an option</mat-label> <mat-select> <mat-option value=& ...

The loading of woff2, woff, and ttf files resulted in a 400 error

Can you explain why the woff2, woff, and ttf files in the "assets" folder are not loading? Any suggestions on how to fix this issue? https://i.sstatic.net/ZMsBw.jpg ...

Leveraging object imported from interface

I have a question regarding Angular/TypeScript. It may seem obvious, but I would like clarification. What I'm doing is creating an interface and exporting it: export interface MainObject { location: string; methodType: string; securityLevel: st ...

Angular not triggering event upon changing URL fragment subscription

I'm currently using Angular 13 and attempting to subscribe to the ActivatedRoute 'fragment'. However, I am facing an issue where the subscription only receives a notification when the page initially loads, and does not update when the fragme ...

I have saved a text file on my device and now I am looking to showcase its contents on an HTML webpage using JavaScript, rather than downloading

I have been working on converting a PDF document into a base64 string. After successfully converting it and saving it to a text file, I am currently able to download the text file. However, I do not want users to have to download the file, but rather dis ...