如何在javascript map对象中获得特定索引的密钥? [英] How do you get the key at specifc index in javascript map object?

查看:739
本文介绍了如何在javascript map对象中获得特定索引的密钥?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下地图对象

const items = new Map([['item1','A'], ['item2','B'], ['item3', 'C']])

我想在索引2中获取密钥。除了使用for循环以获取index = 2的项的键之外,是否有其他方式?

I want to fetch the key at index 2. Is there a way other than using a for loop to get the key of item at index = 2 ?

获得此工作根据答案 -

Got this working as per the answer -

Array.from(items.keys())[2]


推荐答案

地图可能会被订购,但没有编入索引。获取 n 项目的唯一方法是循环。

Maps might be ordered, but they are not indexed. The only way to get the nth item is a loop.

这篇关于如何在javascript map对象中获得特定索引的密钥?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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