json - 从Angular中的Json文件中按特定字段获取数据
问题描述
JSON文件具有以下结构
localjson.json
{
"Product" :{
"data" : [
{ "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;
});
})
);
}
水果.ts
export class Fruits{
itemID: string;
name: string;
qty: string;
}
它说 TypeError: items.find is not a function
解决方案
试试这样
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;
});
})
);
}
推荐阅读
- python - 使用 Python 和 Selenium 获取元素值并进行比较
- android - MSAL Android:在获取令牌时缺少所需的类型令牌:{0}
- visual-studio - HTTP 错误 500.0 - .NetCore 5 的 ANCM 进程内处理程序加载失败
- python - 如何在丢弃单词的同时拆分字符串中的名称?
- image - 如何在 React Js 中创建图像数组
- android - Android app productFlavor versionCode OpenGL ES
- javascript - 传单地图:使用坐标数组绘制的中断路线,如果数组值为 0
- snowflake-cloud-data-platform - 雪花:外部应用/横向
- kotlin - 如何将视图边距绑定到视图模型字符串实时数据?
- angular - 如何在 Angular FontAwesone 中正确堆叠 fa 图标