如果我的鍵值是像本示例這樣的對象,如何從映射對象獲取鍵值?const map1 = new Map();map1.set({x:0,y:0}, 'val');console.log(map1.get({x:0,y:0}));//output: undefined我正在創(chuàng)建一個鍵值的地圖 key 是一個點(diǎn),一個 x 和 y 點(diǎn)的對象 val 是一個生物,因?yàn)檫@個原因等于“測試”。我需要在代碼中更改什么才能從該關(guān)鍵對象獲取該測試值?class Board { constructor() { this.map = new Map(); } add(point, creature) { this.map.set(point, creature) } getVal(aX, aY) { console.log(this.map) // Map { Point { x: 0, y: 0 } => Creature {} } console.log(new Point(aX, aY)) //output: {x:0,y:0} console.log(this.map.get(new Point(aX, aY))) //output: undefined return this.map.get(new Point(aX, aY)) }}class Point { constructor(aX, aY) { this.x = aX; this.y = aY; }}function test() { let board = new Board(); board.add(new Point(0, 0), 'test'); return cretureFromBoard = board.getVal(0, 0);}console.log('test()', test())
如果鍵是對象js,如何從map中的鍵獲取值
ITMISS
2023-07-06 14:49:13