我必须通过provider获取json并将其保存在键为“ id”且其他值为对象的地图中。 这是我的json:
{
"data": {
"stores": [{
"description": "a1",
"name": "b1",
"id": ["_0010008"]
},
{
"description": "a2",
"name": "b2",
"id": ["_0010007"]
}
]
},
"message": "",
"status": "ok"
}
这是我的提供商。
import {
HttpClient
} from '@angular/common/http';
import {
HttpClientModule
} from '@angular/common/http'
import {
Injectable
} from '@angular/core';
import 'rxjs/add/operator/map';
import {
JsonStoresDataInterface
} from './../../providers/stores/stores';
/*
Generated class for the StoresProvider provider.
See https://angular.io/guide/dependency-injection for more info on providers
and Angular DI.
*/
@Injectable()
export class StoresProvider {
private JsonStoresData = new Map([]);
defaultKey = "_0010008";
url_request = "path/to/json";
constructor(public http: HttpClient) {}
getStoresRemote(s) {
this.http.get < JsonStoresDataInterface > (this.url_request).subscribe(data => {
var json = data.data;
for (var store of json.stores) {
var str = id[0];
var arr = {
name: store.name,
description: store.description,
}
this.JsonStoresData.set(str, arr)
}
});
console.log(this.JsonStoresData);
console.log(this.JsonStoresData.get(this.defaultKey));
}
}
console.log(this.JsonStoresData)可以:
https://i.stack.imgur.com/k1ssA.png
console.log(this.JsonStoresData.get(this.defaultKey))不合适:
Undefined
怎么了?以及如何通过地图键获取对象?
为什么这是重复的?我没有看到我的错误,并在“ answer post”中将其修复