【问题标题】:Unable to retrieve map of key value pairs as value in map无法将键值对的映射检索为映射中的值
【发布时间】:2018-08-24 17:32:23
【问题描述】:

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

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)

我检查了在 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)}

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

【问题讨论】:

    标签: angular dictionary key-value-store


    【解决方案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"));
    

    我创建了一个与您的问题相关的简单示例:

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

    【讨论】:

    • 我明白了。我想你知道我想要做什么。我不想更改键的值,在您的示例中为“1”,而是更改值的 Map 值,即“二”或“三”。我该怎么做?
    • 没关系。我想到了。感谢您的帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-04
    • 2020-05-13
    • 2017-04-27
    相关资源
    最近更新 更多