无法检索键值对映射作为映射中的值
Unable to retrieve map of key value pairs as value in map
我正在努力解决一个简单的问题(我认为)。
问题是这样的。
this.keyss = new Map();
this.summonerHistoryService.getimage().subscribe(champsinfo => {
for(let c of Object.values(champsinfo.data))
{
let values = new Map();
values.set("frequency", 0);
values.set("name", c.name);
values.set("image", c.image.full);
this.keyss.set(c.key, values);
}
let champnum = (103).toString();
this.keyss.set(champnum, (this.keyss.get(champnum)).get("frequency") + 1);
console.log((this.keyss.get(champnum)).get("frequency"));
console.log((this.keyss.get(champnum)).get("name"));
console.log((this.keyss.get(champnum)).get("image"));
});
我想看看 "frequency" 值是否已更改,但首先出现错误 console.log((this.keyss.get(champnum)).get("frequency"));
:
ERROR TypeError: _this.keyss.get(...).get is not a function
at SafeSubscriber._next (champion-stats.component.ts:48)
at SafeSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.SafeSubscriber.__tryOrUnsub (Subscriber.js:195)
at SafeSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.SafeSubscriber.next (Subscriber.js:133)
at Subscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber._next (Subscriber.js:77)
at Subscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber.next (Subscriber.js:54)
at MapSubscriber.push../node_modules/rxjs/_esm5/internal/operators/map.js.MapSubscriber._next (map.js:41)
at MapSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber.next (Subscriber.js:54)
at MapSubscriber.push../node_modules/rxjs/_esm5/internal/operators/map.js.MapSubscriber._next (map.js:41)
at MapSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber.next (Subscriber.js:54)
at FilterSubscriber.push../node_modules/rxjs/_esm5/internal/operators/filter.js.FilterSubscriber._next (filter.js:38)
我检查过在 for(let..of) 中将值从 champinfo.data 保存到 this.keyss 效果很好
Map(141) {"266" => Map(3), "103" => Map(3), "84" => Map(3), "12" => Map(3), "32" => Map(3), …}
size : (...)
__proto__ : Map
[[Entries]] : Array(141)
[0 … 99]
0 : {"266" => Map(3)}
key : "266"
value : Map(3)
size : (...)
__proto__ : Map
[[Entries]] : Array(3)
0 : {"frequency" => 0}
1 : {"name" => "Aatrox"}
2 : {"image" => "Aatrox.png"}
length : 3
1 : {"103" => Map(3)}
2 : {"84" => Map(3)}
我不明白为什么会出现此错误。
感谢您的帮助
控制台日志问题:
console.log((this.keyss.get(champnum)).get("frequency"));
console.log((this.keyss.get(champnum)).get("name"));
console.log((this.keyss.get(champnum)).get("image"));
我创建了一个与您的问题相关的简单示例:
let map1 = new Map(); // parent map
let map2 = new Map(); // child map
map2.set('2', 'two');
map2.set('3', 'three');
map1.set('1', map2); // set map 2 under index 1
map1.set('1', (map1.get('1')).get('2'));
// above code snippet, set map1's index '1' value's index '2' to map1's index '1'
// map 1's index 1 = map2
// map 2's index 2 = 'two'
// (map1.get('1')).get('2') = 'two'
// so, (map1.get('1')) = 'two'
console.log((map1.get('1'))); // this console.log prints 'two'
在您的场景中,
(this.keyss.get(champnum))
仅获取一个值。不是地图。
因此,我们不能从单个值调用 get() 函数。因为它不是地图
所以,
(this.keyss.get(champnum)).get("frequency")
不正确。
我正在努力解决一个简单的问题(我认为)。 问题是这样的。
this.keyss = new Map();
this.summonerHistoryService.getimage().subscribe(champsinfo => {
for(let c of Object.values(champsinfo.data))
{
let values = new Map();
values.set("frequency", 0);
values.set("name", c.name);
values.set("image", c.image.full);
this.keyss.set(c.key, values);
}
let champnum = (103).toString();
this.keyss.set(champnum, (this.keyss.get(champnum)).get("frequency") + 1);
console.log((this.keyss.get(champnum)).get("frequency"));
console.log((this.keyss.get(champnum)).get("name"));
console.log((this.keyss.get(champnum)).get("image"));
});
我想看看 "frequency" 值是否已更改,但首先出现错误 console.log((this.keyss.get(champnum)).get("frequency"));
:
ERROR TypeError: _this.keyss.get(...).get is not a function
at SafeSubscriber._next (champion-stats.component.ts:48)
at SafeSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.SafeSubscriber.__tryOrUnsub (Subscriber.js:195)
at SafeSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.SafeSubscriber.next (Subscriber.js:133)
at Subscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber._next (Subscriber.js:77)
at Subscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber.next (Subscriber.js:54)
at MapSubscriber.push../node_modules/rxjs/_esm5/internal/operators/map.js.MapSubscriber._next (map.js:41)
at MapSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber.next (Subscriber.js:54)
at MapSubscriber.push../node_modules/rxjs/_esm5/internal/operators/map.js.MapSubscriber._next (map.js:41)
at MapSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber.next (Subscriber.js:54)
at FilterSubscriber.push../node_modules/rxjs/_esm5/internal/operators/filter.js.FilterSubscriber._next (filter.js:38)
我检查过在 for(let..of) 中将值从 champinfo.data 保存到 this.keyss 效果很好
Map(141) {"266" => Map(3), "103" => Map(3), "84" => Map(3), "12" => Map(3), "32" => Map(3), …}
size : (...)
__proto__ : Map
[[Entries]] : Array(141)
[0 … 99]
0 : {"266" => Map(3)}
key : "266"
value : Map(3)
size : (...)
__proto__ : Map
[[Entries]] : Array(3)
0 : {"frequency" => 0}
1 : {"name" => "Aatrox"}
2 : {"image" => "Aatrox.png"}
length : 3
1 : {"103" => Map(3)}
2 : {"84" => Map(3)}
我不明白为什么会出现此错误。 感谢您的帮助
控制台日志问题:
console.log((this.keyss.get(champnum)).get("frequency"));
console.log((this.keyss.get(champnum)).get("name"));
console.log((this.keyss.get(champnum)).get("image"));
我创建了一个与您的问题相关的简单示例:
let map1 = new Map(); // parent map
let map2 = new Map(); // child map
map2.set('2', 'two');
map2.set('3', 'three');
map1.set('1', map2); // set map 2 under index 1
map1.set('1', (map1.get('1')).get('2'));
// above code snippet, set map1's index '1' value's index '2' to map1's index '1'
// map 1's index 1 = map2
// map 2's index 2 = 'two'
// (map1.get('1')).get('2') = 'two'
// so, (map1.get('1')) = 'two'
console.log((map1.get('1'))); // this console.log prints 'two'
在您的场景中,
(this.keyss.get(champnum))
仅获取一个值。不是地图。
因此,我们不能从单个值调用 get() 函数。因为它不是地图
所以,
(this.keyss.get(champnum)).get("frequency")
不正确。