Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript map.get return undefined

When I want get Object by key I have undefined each all element.

My Map :

nodesMaps : Map<number, Object> = [
{ key: "1", value: Object },
{ key: "2", value: Object },
{ key: "3", value: Object },
]

When I want get value of this map :

this.nodesMaps.get(1) // return undefined
this.nodesMaps.get("1") // return undefined
like image 572
Matthis.h Avatar asked Jul 31 '26 12:07

Matthis.h


2 Answers

below code for me..

let map = new Map([
    [ "A", "AA" ],
    [ "B", "BB" ],
    [ "C", "CC" ]
]);

console.log(map.get("A"));

check this: https://jsfiddle.net/vipinmpd08/1b68eLdr/91920/

like image 191
Vipindas Gopalan Avatar answered Aug 03 '26 02:08

Vipindas Gopalan


You've messed many things.

  1. Even if you declared type Map, you assigned an Array value.
  2. The array was array of objects, not array of arrays with length 2. etc.

Valid map creation should look like this:

let nodesMap = new Map([
    [1, 'foo'],
    [2, 'bar'],
    [3, 'baz'],
])
    
 console.log(nodesMap.get(1)) // foo
like image 20
Nurbol Alpysbayev Avatar answered Aug 03 '26 02:08

Nurbol Alpysbayev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!