Arrays 如何在Http调用过程中提取特定的键/值?
通过HTTP调用,我得到了一个JSON文件,如下所示:Arrays 如何在Http调用过程中提取特定的键/值?,arrays,angular,typescript,http,Arrays,Angular,Typescript,Http,通过HTTP调用,我得到了一个JSON文件,如下所示: [ { "totalConfirmed": 555, "mainlandChina": 548, "otherLocations": 7, "deltaConfirmed": 555, "totalRecovered": 0, "confirmed": { "total": 555, "ch
[
{
"totalConfirmed": 555,
"mainlandChina": 548,
"otherLocations": 7,
"deltaConfirmed": 555,
"totalRecovered": 0,
"confirmed": {
"total": 555,
"china": 548,
"outsideChina": 7
},
"deaths": {
"total": 17,
"china": 17,
"outsideChina": 0
},
"reportDate": "2020-01-22"
},
{
"totalConfirmed": 654,
"mainlandChina": 643,
"otherLocations": 11,
"deltaConfirmed": 99,
"totalRecovered": 0,
"confirmed": {
"total": 654,
"china": 643,
"outsideChina": 11
},
"deaths": {
"total": 18,
"china": 18,
"outsideChina": 0
},
"reportDate": "2020-01-23"
}
]
由此,我想存储totalConfirmed、death和reportDate的值。
所以我的回报应该是这样的
{
totalConfirmed : [555,654],
death: [17, 18],
dates: ["2020-01-22", "2020-01-23"]
}
以下是我在服务中编写的函数。ts:
但不幸的是,它现在起作用了。我有一个错误:
ERROR TypeError: Cannot read property 'totalConfirmedPerDay' of undefined
输入错误:数据历史记录未初始化
public filteredData(): Observable<History> {
return this.httpClient.get(this.hostURL).pipe(
map(res => {
return {
totalConfirmedPerDay: res.totalConfirmed,
totalDeathPerDay: res.deaths.total,
dates: res.reportDate
};
})
);
输入错误:数据历史记录未初始化
public filteredData(): Observable<History> {
return this.httpClient.get(this.hostURL).pipe(
map(res => {
return {
totalConfirmedPerDay: res.totalConfirmed,
totalDeathPerDay: res.deaths.total,
dates: res.reportDate
};
})
);
制作地图的方法是
map((full:any[]) => { //I don't want full
full.map(res=>{ //I want a "resumed" of full
const obj={ //transform each element of full
totalConfirmed:res.totalConfirmed,
totalDeathPerDay:res.deaths.total,
dates.res.reportDate
}
return obj;
)
return full;
})
制作地图的方法是
map((full:any[]) => { //I don't want full
full.map(res=>{ //I want a "resumed" of full
const obj={ //transform each element of full
totalConfirmed:res.totalConfirmed,
totalDeathPerDay:res.deaths.total,
dates.res.reportDate
}
return obj;
)
return full;
})
您可以通过以下方式进行操作:
return this.httpClient.get(this.hostURL)
.pipe(
map(arr => {
return arr.map(sub => {
return {
totalConfirmed: sub.totalConfirmed,
totalDeathPerDay: sub.deaths.total,
dates: sub.reportDate
};
});
})
)
现在在订阅块中:
.subscribe(res => {
let op: History = {
totalConfirmedPerDay: [],
totalDeathPerDay: [],
dates: []
};
res.forEach(e => {
op.totalConfirmedPerDay.push(e.totalConfirmedPerDay);
op.totalDeathPerDay.push(e.totalDeathPerDay);
op.dates.push(e.dates);
});
});
您可以通过以下方式进行操作:
return this.httpClient.get(this.hostURL)
.pipe(
map(arr => {
return arr.map(sub => {
return {
totalConfirmed: sub.totalConfirmed,
totalDeathPerDay: sub.deaths.total,
dates: sub.reportDate
};
});
})
)
现在在订阅块中:
.subscribe(res => {
let op: History = {
totalConfirmedPerDay: [],
totalDeathPerDay: [],
dates: []
};
res.forEach(e => {
op.totalConfirmedPerDay.push(e.totalConfirmedPerDay);
op.totalDeathPerDay.push(e.totalDeathPerDay);
op.dates.push(e.dates);
});
});
让dataHistory:History={}让dataHistory:History={}不幸的是,你给出的解决方案不起作用。如果您有时间,请使用此链接检查一次:@xdeepakvu不幸的是,您提供的解决方案不起作用。如果您有时间,请使用以下链接查看一次:@xdeepakv