[英]find() doesn't return data, only undefined
I want to find() s_id form this JSON: 我想通过此JSON查找()s_id:
{
"StatusCode": 0,
"StatusMessage": "OK",
"StatusDescription": [
{
"s_id": "11E8C70C8A5D78888E6EFA163EBBBC1D",
"s_serial": "PkMGo",
"active": 0,
},
{
"s_id": "11E8C70FB9D10DB38E6EFA163EBBBC1D",
"s_serial": "UgooX",
"active": 0,
},
{
"s_id": "11E8C7179F85836D8E6EFA163EBBBC1D",
"s_serial": "IiLnM",
"active": 0,
}, .....
{
"s_id": "11E8C71905123F1A8E6EFA163EBBBC1D",
"s_serial": "LVpcP",
"active": 0,
}
}]}
I try to find in this but return undefined. 我尝试在此找到但返回未定义。
sensors: Sensors[]=[];
hbp: HBP;
sensor: Sensors;
this.ss.getAllS().subscribe(
sensors => {
this.sensors = sensors
let ss = this.sensors.find(x => x.s_id === this.hbp.s_id);
console.log(ss)
if (ss) {
this.selectedSensor = ss.s_id
}
}
);
and 和
selectedSensor : string = this.sensors.filter(
x => x.s_id === this.sensor.s_id[0])
.map(y => y.s_serial).join('');
I think that I have problem in this line: 我认为我在这方面有问题:
let ss = this.sensors.find(x => x.s_id === this.hbp.s_id);
because, hbp return this json: 因为,hbp返回此json:
active: 0
homebox_id: "11E8C71154CC8C188E6EFA163EBBBC1D"
sensors: Array(2)
0: {s_serial: "s_id", s_id: "11E8C70C8A5D78888E6EFA163EBBBC1D"}
1: {s_serial: "UgooX", s_id: "11E8C70FB9D10DB38E6EFA163EBBBC1D"}
and in this line maybe will find inside this sensors 在这一行中,也许会发现该传感器内部
This selectedSensor
I used in html code 我在html代码中使用过的selectedSensor
<input formControlName="s_id" type="text" placeholder="Select " [(ngModel)]="selectedSensor" aria-label="Number" matInput
[matAutocomplete]="auto">
How to return data ? 如何返回数据?
Can you ask me any idea please? 你能问我任何想法吗?
Use can use find
function like below 使用可以使用如下find
功能
let ss = this.sensors.find((item) => {
return item.s_id === this.hbp.s_idl
})
if (ss) {
this.selectedSensor = ss.s_id
}
If you are looking for filter,map and join combination , Please make sure you are returning the item.property inside map 如果您要查找过滤器,地图和联接组合,请确保返回地图中的item.property
.filter((item) => {
return item.s_id === ss.s_id
}).map(
(item) => {
return item.s_serial
}
).join( )
Check this sample code 检查此示例代码
https://stackblitz.com/edit/arrayfind?file=index.js https://stackblitz.com/edit/arrayfind?file=index.js
Your code seems fine to me, are you sure that this.ss.getAllS()
method returns an array of StatusDescription types? 您的代码对我来说似乎很好,您确定this.ss.getAllS()
方法返回一个StatusDescription类型的数组吗? I would recommend to debug what is actually in sensors
variable at runtime. 我建议在运行时调试sensors
变量中的实际内容。
The observable: sensors, will initially be null, and you can't access properties of null to compare anything to. 可观察的:传感器最初将为null,并且您无法访问null的属性以进行任何比较。
Instead, do a quick null check on the sensors value, then do your find operation: 相反,对传感器值进行快速的空检查,然后执行查找操作:
this.ss.getAllS().subscribe(sensors => {
if (sensors) {
this.selectedSensor = this.sensors.find(x => x.s_id === this.hbp.s_id)
}
});
Here's a StackBlitz example: https://stackblitz.com/edit/select-id-from-observable 这是一个StackBlitz示例: https ://stackblitz.com/edit/select-id-from-observable
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.