如何在ES6(EcmaScript 2015)中获取Set的第一个元素 [英] How to get the first element of Set in ES6 ( EcmaScript 2015)

查看:442
本文介绍了如何在ES6(EcmaScript 2015)中获取Set的第一个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ES6中,如何快速获取元素?

In ES6, how do we quickly get the element?

设置的MDN语法,我没有找到答案。

推荐答案

他们似乎没有暴露该列表是从实例化的对象访问。这是从EcmaScript草案:

They don't seem to expose the List to be accesible from the instanced Object. This is from the EcmaScript Draft:


23.2.4设置实例的属性

23.2.4 Properties of Set Instances

设置实例是从Set原型继承属性的普通对象。设置实例还有一个[[SetData]]内部插槽。

Set instances are ordinary objects that inherit properties from the Set prototype. Set instances also have a [[SetData]] internal slot.

[[SetData]]是值的列表,持有。

[[SetData]] is the list of Values, the Set is holding.

可能的解决方案(一个有点贵的一个)是抓取一个迭代器,然后调用 next()为第一个值:

A possible solution (an a somewhat expensive one) is to grab an iterator and then call next() for the first value:

var x = new Set();
x.add(1);
x.add({ a: 2 });
//get iterator:
var it = x.values();
//get first entry:
var first = it.next();
//get value out of the iterator entry:
var value = first.value;
console.log(value); //1

值得一提的是:

Set.prototype.values === Set.prototype.keys

这篇关于如何在ES6(EcmaScript 2015)中获取Set的第一个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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