Reputation: 55
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>
Upvotes: 0
Views: 120
Reputation: 50797
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.
Upvotes: 2