我在角度js中有点新,我想将过滤后的数据从服务返回到组件。
这是我的json看起来像
{
"emails": [
{
"list": [
{
"id": 1,
"subject": "Test Subject 1"
},
{
"id": 2,
"subject": "Test Subject 2"
}]
},
{
"list": [
{
"id": 3,
"subject": "Test Subject 3"
},
{
"id": 4,
"subject": "Test Subject 4"
}]
}
]
}
这是我的服务
export class EmailService {
emails: any
constructor(private http: Http) {}
getEmailItem(id: number | string) {
return this.http
.get('../../../assets/js/data/email-list.json')
.map(data => {data.json(); return data.json();});
}
}
我希望返回如下数据,我该怎么做?
{ "id": 1, "subject": "Test Subject 1" },
任何指南都将不胜感激,谢谢
答案 0 :(得分:1)
你可以展开你的json,然后应用过滤器,
getEmailItem(id: number | string) {
let flatData = [];
return this.http
.get('../../../assets/js/data/email-list.json')
.map(data => {data.json(); return data.json();})
.map(this.flatEmailList)
.filter(email => email.id == id);
}
flatEmailList(data){
let flatData = [];
data.emails.map(emails => flatData = flatData.concat(emails.list));
return flatData;
}
希望这会有所帮助!!