如何将地图转换为设置,反之亦然?
foreach()
方法使用value
变量两次。写道是为了确保地图和设置的兼容性。
这是与MAP的兼容性,在该地图上通过的回调foreach有三个参数。肯定看起来有些奇怪。但可能有助于在某些情况下轻松替换设置的地图,反之亦然。
let set = new Set(["orange", "apple", "banana"]);
set.forEach((value, valueAgain, set) => {
console.log(valueAgain); //"orange", "apple", "banana"
});
因此,我的问题是如何将设置转换为集合,反之亦然?据我了解,这种情况没有特殊方法。
forEach()
method uses value
variable twice. It's written that this is done in order to ensure the compatibility of Map and Set.
That’s for compatibility with Map where the callback passed forEach has three arguments. Looks a bit strange, for sure. But may help to replace Map with Set in certain cases with ease, and vice versa.
let set = new Set(["orange", "apple", "banana"]);
set.forEach((value, valueAgain, set) => {
console.log(valueAgain); //"orange", "apple", "banana"
});
So, my question is how Set can convert into Set and vice versa? As I understand, there is no special method for this case.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
关键不是您可以轻松地用代码中的地图替换一组。如果这样做,您无论如何都可以触摸代码,并且也不会重写迭代。
真正的好处是,它允许编写可以立即处理两个可以处理的代码。在您的示例中:
The point is not that you can easily replace a set with a map in your code. If you do that, you touch the code anyway, and would have no trouble also rewriting the iteration.
The real benefit is that it allows writing code that can handle both at once. In your example:
关于转换,它没有说什么。
它说您可以使用
MAP
和set
的相同功能回调。It does not say anything about conversion.
It says that you can use the same function callback for the
Map
and theSet
.