javascript 迭代集合元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16401216/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Iterate over set elements
提问by Randomblue
I have turned on the Chrome flag for experimental ECMAscript 6 features, one of which is Set
. As I understand, the details of Set
are broadly agreed upon by the spec writers.
我已经为实验性 ECMAscript 6 功能打开了 Chrome 标志,其中之一是Set
. 据我了解,Set
规范编写者广泛同意的细节。
I create a set a
and add the string 'Hello'
我创建一个集合a
并添加字符串'Hello'
a = Set();
a.add('Hello');
but how do I iterate over the elements of a
?
但是我如何迭代 的元素a
?
for(let i of a) { console.log(i); }
gives "SyntaxError: Illegal let
declaration outside extended mode"
给出“语法错误:let
扩展模式外的非法声明”
for(var i of a) { console.log(i); }
gives "SyntaxError: Unexpected identifier"
给出“语法错误:意外的标识符”
for(var i in a) { console.log(i); }
gives Undefined
给 Undefined
Is it possible to iterate over of a set in Chrome 26?
是否可以在 Chrome 26 中迭代一组?
采纳答案by Vivin Paliath
The of
operator doesn't appear to be currently supported in Chrome. It seems that only FireFox versions 13 through 18 support it. It also appears that none of the browsers actually support Set
although the page does say that some of the tests represent existence and not full functionality or coherence. So it might be that Set
is partially implemented in Chrome.
Chrome 目前似乎不支持该of
运算符。似乎只有 FireFox 版本 13 到 18 支持它。尽管页面确实说某些测试代表存在而不是完整的功能或连贯性,但似乎没有任何浏览器实际支持。所以它可能是在 Chrome 中部分实现的。Set
Set
回答by luschn
A very easy way is to turn the Set into an Array first:
一个非常简单的方法是先将 Set 变成一个数组:
let a = new Set();
a.add('Hello');
a = Array.from(a);
...and then just use a simple for loop.
...然后只需使用一个简单的 for 循环。
Be aware that Array.from
is not supported in IE11.
请注意,Array.from
在 IE11 中不受支持。
回答by FranXh
I use the forEach(..);
function. (documentation)
我用这个forEach(..);
功能。(文档)
回答by bizi
Upon the spec from MDN, Set
has a valuesmethod:
根据 MDN 的规范,Set
有一个values方法:
The values() method returns a new Iterator object that contains the values for each element in the Set object in insertion order.
values() 方法返回一个新的 Iterator 对象,该对象包含 Set 对象中按插入顺序排列的每个元素的值。
So, for iterate through the values, I would do:
因此,为了迭代这些值,我会这样做:
var s = new Set(['a1', 'a2'])
for (var it = s.values(), val= null; val=it.next().value; ) {
console.log(val);
}
回答by Mike Samuel
Even if the syntactic sugar for iteration hasn't been implemented yet, you can probably still use iterators.
即使迭代的语法糖还没有实现,你可能仍然可以使用迭代器。
http://www.2ality.com/2012/06/for-of-ff13.htmlexplains
http://www.2ality.com/2012/06/for-of-ff13.html说明
The special method
__iterator__
returns an iterator object. Such an object has a methodnext()
that either returns the next element in the current iteration sequence or throwsStopIteration
if there are no more elements.
特殊方法
__iterator__
返回一个迭代器对象。这样的对象有一个方法next()
,要么返回当前迭代序列中的下一个元素,要么在StopIteration
没有更多元素时抛出。
So you should be able to iterate over the set using
所以你应该能够使用
for (var it = mySet.__iterator__();;) {
var element;
try {
element = it.next();
} catch (ex) {
if (ex instanceof StopIteration) {
break;
} else {
throw ex;
}
}
// Do something with element
}
You can also define a functional version of for…of like
您还可以定义 for...of like 的功能版本
function forOf(collection, f) {
// jQuery.each calling convention for f.
var applyToElement = f.bind(/* this */ collection, /* index */ void 0);
for (var it = mySet.__iterator__();;) {
var element;
try {
element = it.next();
} catch (ex) {
if (ex instanceof StopIteration) {
break;
} else {
throw ex;
}
}
// jQuery.each return convention.
if (applyToElement(element) === false) { break; }
}
}
回答by nik.ss
this worked for me
这对我有用
mySet.forEach(async(item) =>{
await doSomething(item)
})
回答by Tech
@bizi's answeris close but it did not work for me. This worked on Firefox:
@bizi 的答案很接近,但对我不起作用。这适用于 Firefox:
var s= new Set([1,2]),
it = s.values();
for (var val= it.next().value; val=it.next().value;) {
console.log("set: "+val);
}