以角度按ID从JSON文件检索数据。TypeError:items.find不是一个函数
试图从JSON文件中按id获取数据,但在控制台中出现错误以角度按ID从JSON文件检索数据。TypeError:items.find不是一个函数,json,angular,nested,find,typeerror,Json,Angular,Nested,Find,Typeerror,试图从JSON文件中按id获取数据,但在控制台中出现错误 TypeError: items.find is not a function { "product" :{ "data" : [ { "itemID" : "1" , "name" : "pen" , "qty" : "8" }`, { "itemID" : "2" , "name" : "notepad" , "qty" : "5" } ] } } 我正在使用的函
TypeError: items.find is not a function
{
"product" :{
"data" : [
{ "itemID" : "1" , "name" : "pen" , "qty" : "8" }`,
{ "itemID" : "2" , "name" : "notepad" , "qty" : "5" }
]
} }
我正在使用的函数,其中TypeError:items.find不是控制台中的函数
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;
}
getitems(itemID:string){
返回此.http.get('assets/localjson.json')
.烟斗(
地图((项目:数组)=>{
返回项目。查找((项目:水果)=>{
返回项。itemID==itemID;
});
})
);
}
office.ts
导出类officeitems{
itemID:字符串;
名称:字符串;
数量:字符串;
}
您需要正确使用您的结构。您的结构是一个对象,您已将其视为一个数组
因此,您需要从product属性获取数据,因为数据就是您的数组
您需要更改getItems方法并修复定义的接口
map((产品:any)=>{//{
返回项。itemID==itemID;
});
你需要正确使用你的结构。你的结构是一个对象,你把它当作一个数组 因此,您需要从product属性获取数据,因为数据就是您的数组 您需要更改getItems方法并修复定义的接口
map((产品:any)=>{//{
返回项。itemID==itemID;
});
尝试
map(res:any)=>res.product.data.find((item:Fruits)=>item.itemID==itemID)
非常感谢,它很有效。尝试map(res:any)=>res.product.data.find((item:Fruits)=>item.itemID==itemID)
非常感谢,它很有效。
{
"product" :{
"data" : [
{ "itemID" : "1" , "name" : "pen" , "qty" : "8" }`,
{ "itemID" : "2" , "name" : "notepad" , "qty" : "5" }
]
}
}