最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

node.js - Using a JavaScript Map with array as key, why can't I get the stored value? - Stack Overflow

programmeradmin2浏览0评论

My code initializes a Map object and uses arrays as key. When I try to use the map.get() method, I get "undefined" instead of the value I expect. What am I missing?

const initBoardMap = () => {
  let theBoard = new Map()
  for (let r = 0; r < 3; r++) {
    for (let c = 0; c < 3; c++) {
      //create a Map and set keys for each entry an array [r,c]
      //set the value to a dash
      // ---- mented out the array as key :-(
      //theBoard.set([r, c], '-')
      const mykeyStr = r + ',' + c
      theBoard.set(mykeyStr, '-')
    }
  }
  return theBoard
}

const printBoardMap = theBoard => {
  for (let r = 0; r < 3; r++) {
    let row=''
    for (let c = 0; c < 3; c++) {
      //initialize an array as the map key
      // ment out array as key
      // let mapKey = [r, c]
      //
      //why can't I get the value I expect from the line below?
      //
      //let square = theBoard.get(mapKey)
      //log the value of map.get --- notice its always undefined   
      const mykeyStr = r + ',' + c
      row += theBoard.get(mykeyStr)
       if (c < 2) row += '|'
    }
    console.log(row)
  }
}
let boardMap = initBoardMap()

printBoardMap(boardMap)

My code initializes a Map object and uses arrays as key. When I try to use the map.get() method, I get "undefined" instead of the value I expect. What am I missing?

const initBoardMap = () => {
  let theBoard = new Map()
  for (let r = 0; r < 3; r++) {
    for (let c = 0; c < 3; c++) {
      //create a Map and set keys for each entry an array [r,c]
      //set the value to a dash
      // ---- mented out the array as key :-(
      //theBoard.set([r, c], '-')
      const mykeyStr = r + ',' + c
      theBoard.set(mykeyStr, '-')
    }
  }
  return theBoard
}

const printBoardMap = theBoard => {
  for (let r = 0; r < 3; r++) {
    let row=''
    for (let c = 0; c < 3; c++) {
      //initialize an array as the map key
      // ment out array as key
      // let mapKey = [r, c]
      //
      //why can't I get the value I expect from the line below?
      //
      //let square = theBoard.get(mapKey)
      //log the value of map.get --- notice its always undefined   
      const mykeyStr = r + ',' + c
      row += theBoard.get(mykeyStr)
       if (c < 2) row += '|'
    }
    console.log(row)
  }
}
let boardMap = initBoardMap()

printBoardMap(boardMap)
Share Improve this question edited Nov 19, 2018 at 3:42 lbrucel asked Nov 19, 2018 at 3:05 lbrucellbrucel 831 silver badge7 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

When you pass in a non-primitive to .get, you need to have used .set with a reference to the exact same object. For example, while setting, you do:

  theBoard.set([r, c], '-')

This creates an array [r, c] when that line runs. Then, in printBoardMap, your

  let mapKey = [r, c]

creates another array [r, c]. They're not the same array; if orig were the original array, mapKey !== orig.

You might consider setting and getting strings instead, for example '0_2' instead of [0, 2]:

theBoard.set(r + '_' + c, '-')

and

const mapKey = r + '_' + c;

(best to use const and not let when possible - only use let when you need to reassign the variable in question)

发布评论

评论列表(0)

  1. 暂无评论