Integrating Immutable.js with Angular 2

Looking to optimize performance in your Angular 2 app with immutable.js?

Although my app is functioning properly, I am aiming to enhance its performance through optimization and refactoring. I recently discovered immutable.js and want to convert the data retrieved from an HTTP request into immutable data before utilizing it in an Angular *ngFor loop.

While I can see the data returned by doing a console log, I am encountering difficulties when attempting to map the data to an immutable object. The structure I'm seeing is map->rout->entries->Array[]->Array[], and my data is located in the last array element. How can I access it properly?

// component.ts
import { Component, OnInit} from '@angular/core';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/throw';
import 'rxjs/add/operator/map';
import { List, Map } from 'immutable'; 
import * as Immutable from 'immutable'; 

@Component({
  selector: 'app',
  templateUrl: './component.html',
  styleUrls: ['./component.css'],
})

export class MyComponent implements OnInit {

  let myData:any = null;
  constructor(private http: Http) {
    this.callData("test.json");
  }

  callData(targetUrl) {
      return this.http.get(targetUrl))
      .map((res) => res.json())
      .subscribe(
        (data) => {
         this.myData = [];
         this.myData = Immutable.Map(data);
         console.log(data); // RETURNS THE DATA
         console.log(this.myData); // RETURNS MAP
      });
  }
}
// component.html
   <div *ngIf="myData != null">
       <div *ngFor="let key of myData; let i = index">
          <div class="someClass">
             {{myData[key].something}}
          </div>
       </div>
  </div>

Answer №1

When utilizing Immutable.js, it is recommended to access immutable objects or arrays by utilizing the get method.

Include the following code snippet in your template: {{myData.get(key).something}}

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

Discovering the minimum and maximum within an array containing objects

I am working with an array of objects that contain latitude and longitude points: var pts = [ { "X": 52.67528921580262, "Y": 8.373513221740723 }, { "X": 52.6759657545252, "Y": 8.374114036560059 }, { "X": 52.682574466310314, "Y": 8.372569084 ...

Issue TS2365: The operation of addition cannot be performed between an array of numbers and the value -1000

I'm encountering an error in my ng serve logs despite the function functioning properly with no issues. However, I am concerned as this may pose a problem when building for production. How can I resolve this error? uuidv4() { return ([1e7]+-1e3+- ...

Navigating the update and deletion of master-detail relationships in JSON structures

We are receiving the JSON Data in the following format, $invoice_data = { "CONTACT_ID": "1", "INV_SERIAL_NO": "100", "NAME": "baby", "INV_DATE": "2018-06-27", ...

Integrating meta tags into Angular Universal through subscription

I am facing challenges with dynamically setting meta tags in my Angular application. I am able to set the tags in the ngOnInit method without any issues, but when I try to use a Subscription, the addTag method doesn't work as expected. export class Ap ...

Nest.js: initializing properties from a superclass in a controller

I have a question about unit testing controllers in the Nest.js framework. My issue is that the property from a superclass is not initialized in the controller class when creating a test module. Here is an example of the code I am referring to: export cl ...

When working with Typescript, it is important to correctly identify elements as checkboxes in order to avoid any red underlines when referencing

When trying to check the input type of an element, such as a checkbox in TypeScript, I encounter the issue of the 'element.checked' property being underlined in red. The code snippet below shows how I attempt to address this: element: HTMLElemen ...

Error TS2339: The 'email' property is not found in the 'FindUserProps' type

interface FindUserEmailProps { readonly email: string } interface FindUserIdProps { readonly id: string } type FindUserProps = FindUserEmailProps | FindUserIdProps export const findUserByEmail = async ({ email }: FindUserProps): Promise<IUser&g ...

Issue with [ ] and inability to execute if-statement in the code

I'm facing some difficulties with the code snippet below, specifically with the if-statement and 2D arrays. Here's a breakdown of the issues I've encountered: int[][]image = { {0,0,2,0,0,0,0,0,0,0,0,2}, {0,0,0,0,0,0,0,0, ...

The function is not dynamically allocating an array of strings passed into it

As someone new to c programming, I wanted to create a helper function that could handle reading in files for me. The function should have the following capabilities: Accept a char** as the first input parameter Accept a const char* as the second input pa ...

JavaScript's Blob to Base64 conversion using FileReader is failing to produce any output

In my typescript application, I am utilizing the FileReader to convert a blob into a base64 image for display within the template. adaptResultToBase64(res: Blob): string { let imageToDisplay : string | ArrayBuffer | null = ''; const re ...

Conceal the header and the navigation tabs at the bottom while scrolling - Ionic version 3

Is there a way to conceal the header and footer (tabs at the bottom) as the user begins scrolling down vertically and then bring back the tabs and header when scrolling back up? I would appreciate any tutorials or documentation guides. https://i.sstatic ...

Display or conceal elements within a Component with the help of a Service

After developing a custom Tabs component, I have implemented it in the following way (StackBlitz example): <tabs> <tab title="Tab 1"> <div toolbar> <message><span>Message 1: </span></message> &l ...

Obtain the complete list of all the dates and days within a specified month

Is there a way to retrieve all days and dates for a specific month instead of the current one? I have the code below which currently displays all the days in the current month, but I need to modify it to parse in a specified month. $list=array(); $month ...

How to locate the position of an element within a multi-dimensional array using TypeScript

My data structure is an array that looks like this: const myArray: number[][] = [[1,2,3],[4,5,6]] I am trying to find the index of a specific element within this multidimensional array. Typically with a 1D array, I would use [1,2,3].indexOf(1) which would ...

Using @RequestBody with a Map<String, String>[] in Spring Boot results in a collection of blank elements

My client side application is built with Angular. I am attempting to send a List of Map<String, String> to a Spring Boot REST API, but it seems to be getting serialized as a List of empty items. I have also tried using an array Map<String, String& ...

I am working on a project where I have a parent component that contains a textarea element. My goal is to dynamically adjust the height of this textarea

Is there a way to adjust the size of a textarea component in a child component? textarea.html <textarea style = "height"=150px></textarea> // The height is defined globally here xyz.html <app-textarea></app-textarea> // Looking ...

Unable to retrieve the request body with bodyParser

I am currently working on a NextJS React Application. Within my server/index.tsx file, I have the following code: import next from 'next'; import express from 'express'; import compression from 'compression'; import bodyParser ...

Tips for solving a deliberate circular dependency in an angular provider

If the existing injection token for this provider is available, I want to use it. Otherwise, I will use the specified provider. Below is the code snippet: providers: [ { provide: DesignerRecoveryComponentStore, useFacto ...

Facing the error "Routes not found in Angular 2: ''"

Having an issue with my Angular 2 app (RC4) where upon opening the index.html file, I encounter the error message Error: Cannot match any routes: ''. I've seen this problem mentioned before, but none of the solutions provided seem to work fo ...

An unfamiliar data type is provided as a number but is treated as a string that behaves like a number

Here is the code snippet in question: let myVar = unknown; myVar = 5; console.log((myVar as string) + 5); Upon running this code, it surprisingly outputs 10 instead of what I expected to be 55. Can someone help me understand why? ...