Retrieving a value in the app component from a map using Angular

I have been struggling to display the values of ValueM, ValueR, and product in my app.component.html file. Can anyone offer a solution or tip to help me move forward? Thank you very much.

app.component.ts

forkJoin(
      this.service1.method1(filter1),
      this.service2.methodo2(filter2),
      ).subscribe(data => { 

        const cc = data[0] || [];
        console.log(cc);
        const pp = data[1] || [];

        const arrayIdsProducts = pp.map(element => element.product);
        const sc = cc.filter(elemente => arrayIdsProducts.includes(elemente.product));
        console.log(sc);

        this.valuesC = sc.map(e => ({
        valueM: e.valueM,
        valueR: e.valueR,
        product: e.product
      }));

Console.log

[{…}] 0: codigoSistemaFormatado: "0002.0004", id: 119 product: 5, productName: "T-SHIRT XYZ BLUE XL"

[{…}] 0: product: 5, ValueM: 31.053333333333335, valorR: 49.9

app.compontent.html

  <table formArrayName="productos" class="table table-bordered table-striped">
    <thead>
      <tr>
        <th width="5%" class="text-center">ValueM</th>
        <th width="30%" class="text-center">ValueR</th>
        <th width="9%" class="text-center">Produte</th>
        <th width="7%"></th>
      </tr>
    </thead>
<tr [formGroupName]="i" *ngFor="let item of formGroup.controls.produtos.controls; let i=index; last as isLast; first as isFirst">

      <td>
        {{i+1}}
      </td>

      <input hidden formControlName="unidadeNome">
      <input hidden formControlName="codigoSistemaFormatado">
      <input hidden formControlName="ValueM"> >
      <input hidden formControlName="valueR"> 

      
      <td>
        <app-vo-filtro-produtos (valueSelected)="onProductChanged($event, i)" [showLabel]="false" [multiple]="false"
          formControlName="produto" [itens]="produtos[i]"></app-vo-filtro-produtos>
      </td>

      <td>
        <input type="text" value="{{produtos[i].codigoSistemaFormatado}}" class="form-control" disabled="true">
      </td>

      <td>
        <input type="text" value="{{produtos[i].unidadePrincipal?.unidade.sigla}}" class="form-control" disabled="true">
      </td>


      <td>
        <input type="text" value="{{valueM HERE}}" class="form-control" disabled="true">
      </td>

      <td>
        <input type="text" value="{{valueR HERE}}" class="form-control" disabled="true">
      </td>
       </td>
    </tr>
  </table>
</form>

Answer №1

In order to access the dynamically assigned value of valuesC, you should retrieve it in this manner:

<input type="text" [value]="valuesC.valueR" >

Update:

If my understanding is correct, you will have a separate array as valueC containing mappings of productId values with valueR and valueC.

It would be better to create distinct methods in your typescript file to extract product-specific values from the valueC array like so:

Typescript file:

getValue(productId, key) {
      for(let product of this.valueC) {
        if(productId == product.product) {
          return product[key];
        }
      }
   }

Then reference these methods in your HTML file like this:

<input type="text" [value] = "getValue(<REPLACE_WITH_PRODUCT_ID>, 'valueM')">
<input type="text" [value] = "getValue(<REPLACE_WITH_PRODUCT_ID>, 'valueR')">

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

I'm wondering if you have any insights on how to retrieve objects using Mono WebAssembly

I am looking to send a c# object back via mono-wasm, but when I attempt to do so, the returned object appears in my JavaScript file here. The C# code can be found in [image2]: C# code My JavaScript code can be found here: JS code Everything seems to wor ...

Is it possible to create an instance in TypeScript without using class decorators?

As per the definition on Wikipedia, the decorator pattern enables you to enhance an object of a class with additional functionalities, such as: let ball = new BouncyBall(new Ball()) The Ball object is adorned with extra features from the BouncyBall class ...

Best practices for utilizing child component methods within a parent component in VUE

I am working with the ImageUpload.vue component, which is a straightforward Bootstrap modal containing a few methods. I am currently exploring the best approach to utilize one of these methods. Could it be implemented like this: const app = new Vue({ ...

Error: The function row.child.hasClass is not a recognized function

My challenge is creating row details for DataTables without using Ajax. I have implemented something like this: $(document).ready(function () { function format ( name, value ) { return '<div>Name: ' + name + '<br /> ...

JavaScript Time and Date Formatting

I'm pretty new to programming, especially in Javascript. Can someone help me simplify my code? I'm trying to create a dropdown for months and an input field for the year. Is there a more efficient way to make the month select box? Also, how can ...

What are some ways to make source code more visually appealing on an HTML/JSP page as it is being

I've recently created a webpage with some Java source code, neatly organized within blocks. However, I'm looking to enhance its appearance so it truly looks like Java code. Take a look at my page's code here for reference: Are there any onl ...

What is the process for invoking a service from a component?

I'm currently facing an issue with my service that is responsible for making HTTP requests and returning responses. The problem arises when I try to display parts of the response on the screen within my component, as nothing seems to be showing up des ...

Instructions on establishing a connection with a MongoDB server using JavaScript

Hello all, I am a complete beginner when it comes to working with mongodb and java script. I am currently trying to figure out how to establish a connection to my local mongodb instance using java script so I can retrieve a list of documents. Does anyone ...

Exploring the functionality of multiple index pages in angularjs

I am trying to access the localhost:3000/admin which is located in my views. The index.html and admin.html are two separate base files, one for users and the other for the admin dashboard respectively. Here is a snippet from my app.routes.js file: angul ...

What could be the reason for this XSS script not making a request to my server?

Currently diving into the realm of XSS attacks to enhance my knowledge on application security. My goal is to extract the user's cookie from a local website and then transmit it to my local server for testing purposes. I've successfully obtained ...

Using AngularJS, I can bind the ng-model directive to asynchronously update and retrieve data from

I am currently working with Angular to develop a preferences page. Essentially, I have a field in a mysql table on my server which I want to connect my textbox to using ng-model through an asynchronous xhr request for setting and fetching data. I attempt ...

What are the reasons behind the significant difference in speed between using Node's Object.create(foo) and new Foo()?

For my Sudoku solver in JavaScript, I decided to take a purely functional approach by using immutable 9x9 puzzle arrays. Every time a new number is inserted, a new array is created. Implementation 1: New SudokuPuzzle In the initial version, I utilized th ...

Universal categories, limitations, and hereditary traits

As a newcomer to Typescript and generics, I am unsure if I have encountered a bug/limitation of Typescript or if I am missing the correct approach to achieve my desired outcome. I have a base class called Widget which is generic and holds a value of type ...

Exploring the World of GiantBomb APIs

I have successfully created an account and obtained my API key. I am looking to implement a basic search functionality on my webpage, where users can enter a search query and click a button to display the game title and image. You can find more informatio ...

Determining the type inference in Typescript based on column data objects

Within my object that describes my table, I have a property called dataFields, which is an array of data with 3 keys - name (a required string), label (a required string), and field (an optional string). Now, I would like to add another property called tes ...

Ever since transitioning to Discord.js 13, why am I suddenly encountering a bug with the messageCreate event?

I'm encountering an issue with my external js file for Events in messageCreate.js. It seems like certain functionalities are not working correctly, even though they were functioning fine with Discord.JS version 12. For instance, when I try to return ...

Is there a way for me to access the response from the PHP file I'm calling with Ajax?

I am just starting to explore ajax and jquery, and I recently found a code online that I'm tweaking for my own use. However, I am struggling with how to handle responses from PHP in this context. The current setup involves ajax POSTing to a php page ...

String interpolation can be used to easily accept numbers with dot separators

Is it possible to create a function that can accept numbers with dot separators? Here are some examples: func('100.000.002.333') // should pass func('10') // should pass func('20') // should pass func('100') // shou ...

Nested Promises in Angular's $q Library

I'm facing an issue with a function that should return a list of favorite locations. Here's the code snippet in question: When I call LocationsFactory.getFavoriteLocations(), it returns a promise like this: getFavoriteLocations: function() { ...

JavaScript data structure similar to ListOrderedMap/ArrayMap

Does anyone know of a JavaScript data structure that functions similarly to ListOrderedMap? I need it to have the capability to add an object at a specific index, retrieve the index for a given object, and lookup an object by its id. Unfortunately, all t ...