问题描述
当地图只有一个成员并且您不知道键是否具有某种迭代地图的循环构造时,这是获取地图价值的唯一方法吗?
Is it true the only way to get value of a Map when it has only one member and you don't know the key is with a looping construct of some kind that iterates the map?
还是有某种速记myMap.item(0)之类的东西?
or is there some kind of shorthand myMap.item(0) sort of thing?
推荐答案
要获得预期的结果,请使用以下选项ES6扩展运算符和[0] [1]的索引,而无需使用键
To achieve expected result, use below option ES6 spread operator and index of [0][1] without using key
var map = new Map()
map.set('contra', 'Asynchronous flow control')
console.log([...map][0][1])
代码示例- https://codepen.io/nagasai/pen/zaerbX
说明:
[... map] 将提供具有键值的数组数组,即 [['contra','异步流控制']]
[... map] [0] 将提供键值数组- ['contra','异步流控制']
[... map] [0] [0] 获取密钥,即'contra'和
[... map] [0] [1] 无需使用键即可获取价值,即异步流控制"
[...map] will provide array of array with key,value i.e [['contra', 'Asynchronous flow control']]
[...map][0] will provide key value array - ['contra', 'Asynchronous flow control']
[...map][0][0] gets key i.e 'contra' and
[...map][0][1] gets value without using key i.e 'Asynchronous flow control'
这篇关于如果地图只有一个成员,如果您不知道密钥,如何在不循环的情况下获取它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!