我有一个json文件-虚拟文件,它看起来像这样-
myMap: any = {};
myObservable: Observable<any> = Observable.of(this.dummy);
getObservable() {
return this.myObservable;
}
ngOnInit() {
this.getObservable()
.subscribe(myResponse => {
this.myMap = new Map(myResponse); //Converting myresponse to map here.
)}.
我想将此json文件转换为具有与json文件中给定的键值对相同的映射。
为此,我创建了一个可观察对象-首先是myObservable,然后在component.ts文件中做了类似的事情-
{
Key1,[alpha,beta,gamma]
Key2,[A,B,C]
Key3,[One,Foo,Bar]
}
但是这种比喻无法奏效。如何将该可观察文件转换为打字稿中的地图?
预期输出:
x % 2
答案 0 :(得分:0)
const dummy = [
{"key":"KEY1", "value":["alpha","beta","gamma"]},
{"key":"KEY2", "value":["A","B","C"]},
{"key":"KEY3", "value":["One","Foo","Bar"]}
]
const result = new Map();
dummy.map(item => {
result.set(item["key"], item["value"]);
})
for (var [key, value] of result.entries()) {
console.log(key + ' = ' + value);
}