无法检索键值对的映射作为映射中的值

时间:2018-08-24 17:32:23

标签: angular dictionary key-value-store

我正在努力解决一个简单的问题(我认为)。 问题是这个。

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"));
});

我想查看“频率”值是否已更改,但最初的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)

我检查了如何将champinfo.data中的值保存到for(let..of)中的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)}

我不明白为什么会出现此错误。 谢谢您的帮助

1 个答案:

答案 0 :(得分:0)

与您的控制台日志有关:

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")不正确。