使用以数组为键的JavaScript Map,为什么不能获取存储的值? [英] Using a JavaScript Map with array as key, why can't I get the stored value?

查看:39
本文介绍了使用以数组为键的JavaScript Map,为什么不能获取存储的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码初始化一个Map对象,并使用数组作为键.当我尝试使用map.get()方法时,我得到的是未定义"而不是我期望的值.我想念什么?

  const initBoardMap =()=>{让theBoard = new Map()for(let r = 0; r< 3; r ++){for(let c = 0; c< 3; c ++){//创建一个Map并为每个条目设置键[r,c]//将值设置为破折号//----将数组注释为键:-(//theBoard.set([r,c],'-')const mykeyStr = r +','+ ctheBoard.set(mykeyStr,'-')}}返回董事会}const printBoardMap = theBoard =>{for(let r = 0; r< 3; r ++){让行=''for(let c = 0; c< 3; c ++){//初始化一个数组作为映射键//注释掉数组作为键//让mapKey = [r,c]////为什么我无法从下面的行中获得期望的值?////让正方形= theBoard.get(mapKey)//记录map.get的值---注意它始终是未定义的const mykeyStr = r +','+ c行+ = theBoard.get(mykeyStr)如果(c< 2)行+ ='|'}console.log(行)}}让boardMap = initBoardMap()printBoardMap(boardMap) 

解决方案

当您将非基元传递给 .get 时,您需要使用 .set 并引用完全相同的对象.例如,在设置时,您可以执行以下操作:

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

该行运行时,此创建数组 [r,c] .然后,在 printBoardMap 中,您的

 让mapKey = [r,c] 

创建另一个数组 [r,c] .它们不是同一阵列;如果 orig 是原始数组,则 mapKey!== orig .

您可以考虑设置并获取字符串,例如'0_2'而不是 [0,2] :

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

  const mapKey = r +'_'+ c; 

(最好使用 const ,而不是 let -仅在需要重新分配有问题的变量时才使用 let )

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
      // ---- commented 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
      // comment 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)

解决方案

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)

这篇关于使用以数组为键的JavaScript Map,为什么不能获取存储的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆