I need to find a value in an array of arrays, but when I use the .find it returns undefined
JavaScript
x
81
81
1
import { Component, OnInit } from '@angular/core';
2
import * as XLSX from 'xlsx';
3
import { ExcelSheetsService } from '../services/excel-sheets.service';
4
5
6
@Component({
7
selector: 'app-hojaexcel',
8
templateUrl: './hojaexcel.component.html',
9
styleUrls: ['./hojaexcel.component.css']
10
})
11
export class HojaexcelComponent implements OnInit {
12
13
14
constructor( private excelsheetService: ExcelSheetsService ) { }
15
16
ngOnInit(): void {
17
}
18
19
20
codigo: any = '';
21
datos: [][] = [];
22
23
24
onFileChange( evt: any ){
25
26
27
28
const target: DataTransfer = <DataTransfer> (evt.target);
29
30
if(target.files.length !== 1) throw new Error ('No se pueden subir varios archivos a la vez');
31
32
const reader: FileReader = new FileReader();
33
34
reader.onload = ( e: any ) => {
35
const bstr: string = e.target.result;
36
37
const wb: XLSX.WorkBook = XLSX.read( bstr, { type: 'binary' } );
38
39
const wsname: string = wb.SheetNames[0];
40
41
const ws: XLSX.WorkSheet = wb.Sheets[wsname];
42
43
this.datos = (XLSX.utils.sheet_to_json(ws, { header: 1 }));
44
console.log(this.datos);
45
46
return this.datos;
47
48
};
49
50
reader.readAsBinaryString(target.files[0]);
51
52
}
53
54
look(): any{
55
56
const found = this.datos.find(element => element == this.codigo );
57
console.log(found);
58
console.log(this.datos);
59
60
61
}
62
63
64
65
66
67
pasardata(){
68
this.excelsheetService.impData( this.datos )
69
.subscribe( resp => {
70
console.log(resp);
71
});
72
73
}
74
75
76
77
78
79
}
80
81
The array of arrays is datos. And this is the Html:
JavaScript
1
25
25
1
<p>Subir Archivo excel</p>
2
<br>
3
4
<input type="file" (change)="onFileChange($event)" multiple="false" />
5
6
<br>
7
8
<button (click)="pasardata()">
9
Subir a base de datos Firebase
10
</button>
11
12
<br>
13
14
<div class="col">
15
<form (ngSubmit)="look()">
16
17
<input type="text" placeholder="Codigo" name="codigo" [(ngModel)]="codigo"/>
18
19
20
<button>Enviar</button>
21
22
</form>
23
</div>
24
25
I have tried to access to the datos array of arrays and it returns an empty array, I want to use the array of arrays to display a table an after this get an specific value of the table, using .find().
Advertisement
Answer
Assuming datos: [][]
is a nested array of strings, you could use flatMap
with find
:
JavaScript
1
4
1
const found = this.datos
2
.flatMap(element => element) // flatten nested array
3
.find(element => element === this.codigo);
4
That being said, if datos
is a nested array of objects, you can’t simply compare {} === {}
, you would need to compare against some kind of property object:
JavaScript
1
4
1
const found = this.datos
2
.flatMap(element => element) // flatten nested array
3
.find(element => element.someProperty === this.codigo);
4
Version without flatMap
:
JavaScript
1
4
1
const found = this.datos
2
.reduce((acc, curr) => acc.concat(curr))
3
.find(element => element === this.codigo);
4
Hopefully that helps!