0

I have an event array and another team array, I need to match them by event = team []. Medical Teams. []. Eq and get the amount in relation to the selected team.

The multiselect gives me these values:

["Equipo", "Data"]

The arrangement with which I have to validate and extract the amount of that equipment is:

[
    {
        "equipo": [
            {
                "eq": "Equipo",
                "monto": "1000"
            } ,
            {
                "eq": "Data",
                "monto": "200,00"
            }
        ] ,
        "_id": "5d9eb311efa054e57755842d",
        "nombre": "Equipos Médicos",
        "__v": 24,
        "codigo": "IUYIUEIUG-984729"
    } ,
    {
        "equipo": [
            {
                "eq": "EM1",
                "monto": "10,00"
            } ,
            {
                "eq": "EM2",
                "monto": "20,00"
            } ,
            {
                "eq": "EM3",
                "monto": "30,00"
            }
        ] ,
        "_id": "5da89260ec2d620ab8fae605",
        "nombre": "Especialidad 2",
        "codigo": "UYTDUWETVUW-87643",
        "__v": 1
    }
]

It has to result in its amount... Expected result:

[1000, 200]

Code try in component.ts:

multiEQ(e) {
    console.log('e: ', e);
    let elementByService = [];

    this.equiposMedicosService.getAll().subscribe(em => {
      console.log('em: ', em);
      em.map(i => {
        elementByService.push(i);
      });
    });

    console.log('elementByService: ', elementByService);
  }

Code in component.html:

<div class="container text-theme">
    <div class="col-12">
    <table class="table table-hover text-theme">    
        <thead>    
            <tr>    
                <th>Acción</th>    
                <th>Clínica u Hospital</th> 
                <th>Piso</th>     
                <th>Consultorio</th>     
            </tr>    
        </thead>
        <tbody formArrayName="clinica">    
            <tr *ngFor="let item of resourceForm.get('clinica').controls;let i = index">   
                <ng-container formGroupName="{{i}}">
                <td (click)="deleteClinica(i)">   
                    <i class="fa fa-trash pointer" style="color:red"></i>    
                </td>
                <td>    
                    <nb-select formControlName="clc"
                            class="w-100"
                            size="small">
                        <nb-option *ngFor="let clinica of clinicas" [value]="clinica._id">{{ clinica.nombre }}</nb-option>
                    </nb-select>
                </td>
                <td>
                    <input type="number"
                            class="form-control text-theme text-right"
                            formControlName="piso"
                            min="0"
                            required/>
                </td>
                <td>
                    <input type="text"
                            class="form-control text-theme text-right"
                            formControlName="consultorio"
                            required/>
                </td>
                </ng-container>
            </tr>    
            <tr>    
                <td (click)="addClinica()">  
                    <i class="fa fa-plus pointer" style="color:green"></i>    
                </td>    
            </tr>    
        </tbody>    
    </table>
    </div>
</div>
  • In [an earlier question](https://stackoverflow.com/questions/59092224) you included all your input data, the output format you would like, and your attempt to achieve it, as well as a little explanation. That was, after a few edits, a well-asked question. You need more here. Your explanation is not clear, you don't show the output you desire for a given input, and you don't demonstrate that you've tried this at all yourself. – Scott Sauyet Dec 06 '19 at 18:09
  • Try with your code and at least it didn't work for me, the previous code was in the backend with nodejs now I sent it to the frontend with angular 8.1 – Denis Alayza Dec 06 '19 at 18:33
  • I am looking for as a result that I return the value of each item and then attach it in an entry that shows the total sum of them. Since I don't know about ramda and I was trying on it, what I did in parallel was a .map that returns me record by record and then I apply .map to return the first record to match. But because they are fixes it doesn't work because it returns two results when I validate the two fixes. – Denis Alayza Dec 06 '19 at 18:46
  • I see you added the desired results. That's good. Can you also add the code you've tried to write, and explain where it fails? – Scott Sauyet Dec 06 '19 at 18:48
  • Add code angular... return the values for analitcs – Denis Alayza Dec 06 '19 at 19:15
  • The Angular markup is (I would hope) irrelevant. But thanks for the JS. I'll take a look. – Scott Sauyet Dec 06 '19 at 19:18
  • Added an answer. You might want to remove the unnecessary Angular markup from the question. The idea on StackOverflow is that questions are here not just for the asker but for future users who might be looking for something similar. So as well as being verifiable and complete, the code in the question should be *minimal*. – Scott Sauyet Dec 06 '19 at 19:40

1 Answers1

2

Something like the following might work. I make no attempt to fit it into Angular. You can alter it to fit there or just call it from Angular with the appropriate parameters.

const getAmounts = (nombre = '', eqs = [], equipos = []) => {
  const entry = equipos.find(({nombre: nom}) => nom == nombre) || {}
  const equipo = entry.equipo || []
  const eqList = eqs.map((eq) => equipo.find(({eq: eq2}) => eq == eq2 ) || {})
  return eqList.map(({monto}) => monto)
}

const equipos = [{equipo: [{eq: "Equipo", monto: "1000"}, {eq: "Data", monto: "200, 00"}], _id: "5d9eb311efa054e57755842d", nombre: "Equipos Médicos", __v: 24, codigo: "IUYIUEIUG-984729"}, {equipo: [{eq: "EM1", monto: "10, 00"}, {eq: "EM2", monto: "20, 00"}, {eq: "EM3", monto: "30, 00"}], _id: "5da89260ec2d620ab8fae605", nombre: "Especialidad 2", codigo: "UYTDUWETVUW-87643", __v: 1}]

console .log (getAmounts ('Equipos Médicos', ['Equipo', 'Data'],    equipos))
console .log (getAmounts ('Equipos Médicos', ['Data'],              equipos)) 
console .log (getAmounts ('Especialidad 2',  ['EM3', 'EM1'],        equipos))
console .log (getAmounts ('Especialidad 2',  ['EM3', 'EM4', 'EM5'], equipos))

If you don't like the undefined values for missing entries, you could replace the last line with return eqList.map(({monto}) => monto || '0') (or whatever default you choose.)

While we could do this with various Ramda techniques, none look like they'd be any more readable than the above. You seem to be focused on Equipos Médicos; if that can be hard-coded, you can remove the nombre parameter, and replace the use of it in the first line with the actual string.

Scott Sauyet
  • 49,207
  • 4
  • 49
  • 103