JSON文件具有以下结构
localjson.json
{
"Product" :{
"item" : [
{ "itemID" : "1" , "name" : "Apple" , "qty" : "3" }`,
{ "itemID" : "2" , "name" : "Banana" , "qty" : "10" }
]
} }
所以通过id获取项目,我有
getfruits(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;
});
})
);
}
Fruits.ts
export class Fruits{
itemID: string;
name: string;
qty: string;
}
它说TypeError:items.find不是函数
尝试这样
export interface Fruits {
itemID: string;
name: string;
qty: string;
}
getfruits(itemID: string) {
return this.http.get<Array<Fruits>>('assets/localjson.json').pipe(
map((items: { Product: { item: Fruits[] } }) => {
return items.Product[0].find((item: Fruits) => {
return item.itemID === itemID;
});
})
);
}