Jav*_*ter 52 javascript ecmascript-6
在ES6中,我们如何快速获得元素?
在MDN语法集中,我没有找到答案.
Min*_*our 61
它们似乎没有公开List可以从instanced Object访问.这来自EcmaScript草案:
23.2.4设置实例的属性
Set实例是从Set原型继承属性的普通对象.设置实例也有[[SetData]]内部插槽.
[[SetData]]是Set所持有的值列表.
一个可能的解决方案(一个有点昂贵的解决方案)是获取迭代器然后调用next()
第一个值:
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
Run Code Online (Sandbox Code Playgroud)
值得一提的是:
Set.prototype.values === Set.prototype.keys
Run Code Online (Sandbox Code Playgroud)
xgq*_*rms 24
for...of
环形const set = new Set();\nset.add(2);\nset.add(3);\n\n// return the first item of Set \xe2\x9c\x85\nfunction getFirstItemOfSet(set) {\n for(let item of set) {\n if(item) {\n return item;\n } \n }\n return undefined;\n}\n\nconst first = getFirstItemOfSet(set);\nconsole.log(\'first item =\', first);
Run Code Online (Sandbox Code Playgroud)\r\ndestructuring assignment
const set = new Set();\nset.add(2);\nset.add(3);\n\n// only get the first item \xe2\x9c\x85\nconst [first] = set;\nconsole.log(\'first item =\', first, typeof first);\n// first item = 2 number
Run Code Online (Sandbox Code Playgroud)\r\n...spread
操作员const set = new Set();\nset.add(2);\nset.add(3);\n\n// convert Set to Array \xe2\x9c\x85\nconst first = [...set][0];\nconsole.log(\'first item =\', first);
Run Code Online (Sandbox Code Playgroud)\r\niterator
&next()
const set = new Set();\nset.add(2);\nset.add(3);\n\n// iterator \xe2\x9c\x85\nconst first = set.keys().next().value;\nconsole.log(`first item =`, first);\n\n// OR\nset.values().next().value;\n\n// OR\nset.entries().next().value[0];\n// OR\nset.entries().next().value[1];
Run Code Online (Sandbox Code Playgroud)\r\nhttps://www.cnblogs.com/xgqfrms/p/16564519.html
\n 归档时间: |
|
查看次数: |
27925 次 |
最近记录: |