簡體   English   中英

無法檢索鍵值對的映射作為映射中的值

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

我想查看“頻率”值是否已更改,但在第一個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)}

我不明白為什么會收到此錯誤。 謝謝你的幫助

控制台日志出現問題:

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()函數。 因為它不是Map,所以(this.keyss.get(champnum)).get("frequency")不正確。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM