How to dynamically loop through an Immutable js Map in Angular templates?

I heard that using Immutable.js in my Angular application can improve performance and I want to incorporate it into my code.

Currently, my component utilizes a regular TypeScript Map in the template, which is iterated with *ngFor and keyValue pipe.

However, when I try to replace the map with an Immutable.Map, the iteration does not work as expected!

So, my question is how can I iterate over an Immutable.Map within the template?

Thank you in advance.

Update: To replicate the issue...

In app.component.ts:

import { Component } from '@angular/core';
import { Map as immuMap } from 'immutable';

@Component({
  selector: 'app-map',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  myMap = immuMap({ a: 1, b: 2, c: 3 });
}

In app.component.html:

<div *ngFor="let item of myMap | keyvalue">
  <span>{{ item.key }} : {{ item.value }}</span>
</div>

Previously, when myMap was a simple TypeScript Map, it worked fine. However, after refactoring to use Immutable.js Map, the ngFor no longer produces the expected result.

The output of the above code snippet is:

__altered : false 
__hash : 
__ownerID: 
_root : [object Object] 
size : 3

Answer №1

When working with immutable collections, there is no need to use the keyvalue pipe in ngFor. Immutable.js Maps can be seamlessly handled by ngFor similar to native javascript Maps.

The entries of a Map's iterator are represented as arrays in the format [key, value]

const imMap = Immutable.Map({ a: 1, b: 2, c:3});
for(entry of imMap ) {
  console.log('entry', entry[0], 'is', entry[1]);
}

const nativeMap = new window.Map([['a',1], ['b',2], ['c':3]]);
for(entry of nativeMap ) {
  console.log('entry', entry[0], 'is', entry[1]);
}

// both loops will output:
// entry a is 1
// entry b is 2
// entry c is 3

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

Tips on excluding the index file from VS Code, Intellisense, and Autocomplete

Currently, I am involved in a project that focuses on developing a library. The project includes a lib.ts file located at the root of the workspace, which outlines all the public exports of the library like this: export * from "./a/a.ts" export * ...

The location of the Aurelia breakpoint needs adjusting

The issue I am encountering involves the aurelia skeleton-typescript-webpack project. When attempting to debug the TypeScript code in Chrome, the breakpoints do not align properly with where the program actually stops. Here's an example: After placin ...

Struggling to make type definitions work in react-hook-form 7

Upon defining the types of my form fields, I encountered an issue where each field seems to take on all three different types. This is puzzling as I have specified 3 distinct types for my 3 different fields. What could be causing this confusion? https://i ...

Currently, I am collaborating on an e-commerce endeavor utilizing TypeScript and sanity.io, encountering an issue along the way

Encountering an Uncaught TypeError message: Cannot read properties of undefined (reading '_ref'). While attempting to utilize code for displaying API content on a webpage, what might be causing this issue and how can it be resolved to successful ...

Monitor changes in a dynamic child component using Angular fire and TypeScript only (no HTML)

Currently, I am developing a component using TypeScript and passing inputs to my child component from there. In the parent TypeScript file: this.childComponent = this.viewContainerRef.createComponent(this.data.body).instance; this.childComponent['chi ...

Using Angular 2 to invoke a component

Currently, I am working on building a new Angular 2 application. Within the home.component.ts file, there is a small form that has been designed as shown in the image linked below https://i.sstatic.net/nimDm.jpg After the user clicks on the Submit button ...

having difficulty interpreting the information from angular's httpclient json request

After creating an Angular function in typescript to make an http request for JSON data and save it to an object, I noticed that the function requires two clicks of the associated button to work properly. Although the connection and data parsing are success ...

"Unlocking the Power of Ionic: A Guide to Detecting Status 302 URL Redirects

Trying to handle a http.post() request that results in a 302 redirect, but struggling to extract the redirected URL. Any tips on how to achieve this? Appreciate any help. ...

Sign up for a service that sends out numerous simultaneous requests for every item in a list

I am encountering an issue with my Angular 5 component that is responsible for displaying a list of items. The items are fetched from a service and populated in the component by subscribing to the service. The service first makes a request to retrieve the ...

The code is throwing an error: Unable to access the 'grower' property as it is undefined

I'm facing an issue with a button that triggers the function 'SaveNewOpportunity' in my component file. When I click the button, I encounter the following error: ERROR TypeError: Cannot read property 'grower' of undefined Here is ...

Having issues with -ms-filter not functioning in IE while using Angular 4

Having trouble with filters not functioning in IE browser when using angular 4. Below is the CSS code snippet: .style{-ms-filter: grayscale(50%); -ms-filter: brightness(70%);} ...

Incorrect Angular Routing Component OpeningI am experiencing an issue where

I am facing an issue with lazy loading a module, where it is not correctly displaying the desired component. Even though the route seems correct, it shows a different component instead. https://i.sstatic.net/v4oAB.png Despite specifying the path for "Pus ...

Angular does not seem to support drop and drag events in fullCalendar

I am looking to enhance my fullCalendar by adding a drag and drop feature for the events. This feature will allow users to easily move events within the calendar to different days and times. Below is the HTML code I currently have: <p-fullCalendar deep ...

It is impossible to determine the 2D position based on the given 3D coordinates

Within my project, I am currently in the process of selecting vertices from a point cloud using angular and three.js. My main goal is to label a selected vertex with its corresponding x, y, z information. In order to achieve this, I have been consulting va ...

Error: You can't use the 'await' keyword in this context

I encountered a strange issue while using a CLI that reads the capacitor.config.ts file. Every time the CLI reads the file, it throws a "ReferenceError: await is not defined" error. Interestingly, I faced a similar error with Vite in the past but cannot ...

I am attempting to retrieve custom cellRendererParams within the CustomCellRenderer class

I'm currently working with Ag-Grid in my angular application and am trying to implement a custom cell renderer. The tutorial I followed uses ICellRendererParams for the parameter type passed to the init event. agInit(params: ICellRendererParams): void ...

Dynamically insert components into table rows with ComponentFactory in Angular version 6

My component contains the following HTML structure: <table> <thead> <tr> <th> </tr> </thead> <tbody> <ng-container #container class="sample"> </ng-container> </tbody> </table ...

Change icons in Ionic 5 when selecting a tab

How can I change my tab icons to outline when not selected and filled when selected? The Ionic 5 Tabs documentation mentions a getSelected() method, but lacks examples on its usage. I plan to utilize the ionTabsDidChange event to detect tab clicks, then ...

Issue with Angualr2: Trying to assign a value to a property of #<AbstractControl> that is read-only

The form structure is as follows: <form [ngFormModel]="myForm" (ngSubmit)="update()"> <ion-label floating>First Name</ion-label> <ion-input type="text" id="fname" [ngFormControl]="fname"> & ...

Angular Universal (Server-Side Rendering) does not pre-render content in specific languages

I have implemented ngx-translate for my multi-language application. app.module.ts import {TranslateLoader, TranslateModule, TranslateService} from '@ngx-translate/core'; export function HttpLoaderFactory(httpClient: HttpClient) { return new ...