【发布时间】:2020-06-24 18:56:06
【问题描述】:
试图通过 id 从 JSON 文件中获取数据,但在控制台中出现错误
TypeError: items.find is not a function
{
"product" :{
"data" : [
{ "itemID" : "1" , "name" : "pen" , "qty" : "8" }`,
{ "itemID" : "2" , "name" : "notepad" , "qty" : "5" }
]
} }
我正在使用的函数 where TypeError: items.find is not a function in console
getitems(itemID: string) {
return this.http.get<Array<Fruits>>('assets/localjson.json')
.pipe(
map((items: Array<any>) => {
return items.find((item: Fruits) => {
return item.itemID=== itemID;
});
})
);
}
office.ts
export class officeitems {
itemID: string;
name: string;
qty: string;
}
【问题讨论】:
-
试试
map(res: any) => res.product.data.find((item: Fruits) =>item.itemID=== itemID) -
非常感谢,它成功了。
标签: json angular nested find typeerror