我可以使用相同的代码遍历 immutable.List、immutable.Set 或 Javascript 数组或集合吗?
Can I loop over an immutable.List, immutable.Set or Javascript Array or Set using same code?
我正在编写一些使用许多不同 "lists" 项的 NodeJS 代码。底层实现使用包括 Immutable.List、Immutable.Set、Javascript 数组和 Javascript 集合。
出于我不会深入探讨的原因,我希望能够在不考虑基础集合的情况下循环遍历这些项目。
一些背景知识可能会有用。对于数组,我可以使用以下循环遍历 Javascript 数组。
let anArray = [ new SomeObj(), new SomeObj(), ... ]
for(let idx=0; idx < anArray.length; idx++){
let someObj = anArray[idx];
// ... etc
if (someCondition) break;
}
对于 Immutable.List,我需要使用 Immutable.List.size 属性(而不是长度 属性)并使用 "Immutable.List.get" 函数作为如下:
const immutable = require('immutable');
let aList = immutable.List([ new SomeObj(), new SomeObj(), ... ]);
for(idx=0; idx < aList.size; idx++){
var item = aList.get(idx);
// ... etc
if (someCondition) break;
}
除了一些小差异外,循环逻辑非常接近。
对于那些建议使用可用于每个对象的 forEach() 方法的自然答案的人。正常的 Javascript Array.forEach() method doesn't handle breaking out of the loop (the Array.some() method is needed for that situation). The Immutable.List is based on a Immutable.Collection.forEach 确实支持 "breaking" 循环,因此这可能有效,但需要将 Javascript 对象包装到不可变的 sequence/collection 中。我刚开始使用 Immutable 库,因此熟悉 Immutable 的人可能还有另一种众所周知的方法。
接下来将介绍我正在考虑的方法。
我正在考虑提供如下解决方案的效用函数:
for(let idx=0, looper=new Looper(aListOrSet); idx < looper.length(); idx++){
var item = looper.get(idx);
// ... etc
if (someCondition) break;
}
我看过的一些内容包括:
- Immutable.Collection.forEach
- Immutable Sequences - 这可能就是诀窍。我必须将 Javascript 对象转换为不可变序列,但这可能有效。
有没有其他人尝试解决这个问题,如果有,他们是如何解决这个问题的?
您可以制作原型并创建自己的列表,该列表的长度函数等于 returns 的大小。您还可以通过原型重写数组上的 forEach 方法以进行 break。
我能想到的唯一另一种不是超级干净的方法是在调用循环对象的函数并将其作为参数发送之前获取对象的长度或大小。
您可以使用 for...of
loop. This allows you operate on any Iterable 对象,其中包括内置集合以及 Immutable.js 集合。
for (let element of collection) {
// do something with element
if (someCondition) {
break;
}
}
它不会像常规 for
循环或 forEach
那样让您访问迭代索引,但这很容易跟踪您自己:
let i = 0;
for (let element of collection) {
// do something with element
if (someCondition) {
break;
}
i++;
}
您可能还想阅读 How to short circuit Array.forEach like calling break。它列出了几个解决方案,包括 for...of
应该适用于您要使用的所有集合:
- 在
try
中包装循环并抛出异常以中断
- 使用
.some()
和return true
打破
我正在编写一些使用许多不同 "lists" 项的 NodeJS 代码。底层实现使用包括 Immutable.List、Immutable.Set、Javascript 数组和 Javascript 集合。
出于我不会深入探讨的原因,我希望能够在不考虑基础集合的情况下循环遍历这些项目。
一些背景知识可能会有用。对于数组,我可以使用以下循环遍历 Javascript 数组。
let anArray = [ new SomeObj(), new SomeObj(), ... ]
for(let idx=0; idx < anArray.length; idx++){
let someObj = anArray[idx];
// ... etc
if (someCondition) break;
}
对于 Immutable.List,我需要使用 Immutable.List.size 属性(而不是长度 属性)并使用 "Immutable.List.get" 函数作为如下:
const immutable = require('immutable');
let aList = immutable.List([ new SomeObj(), new SomeObj(), ... ]);
for(idx=0; idx < aList.size; idx++){
var item = aList.get(idx);
// ... etc
if (someCondition) break;
}
除了一些小差异外,循环逻辑非常接近。
对于那些建议使用可用于每个对象的 forEach() 方法的自然答案的人。正常的 Javascript Array.forEach() method doesn't handle breaking out of the loop (the Array.some() method is needed for that situation). The Immutable.List is based on a Immutable.Collection.forEach 确实支持 "breaking" 循环,因此这可能有效,但需要将 Javascript 对象包装到不可变的 sequence/collection 中。我刚开始使用 Immutable 库,因此熟悉 Immutable 的人可能还有另一种众所周知的方法。
接下来将介绍我正在考虑的方法。 我正在考虑提供如下解决方案的效用函数:
for(let idx=0, looper=new Looper(aListOrSet); idx < looper.length(); idx++){
var item = looper.get(idx);
// ... etc
if (someCondition) break;
}
我看过的一些内容包括:
- Immutable.Collection.forEach
- Immutable Sequences - 这可能就是诀窍。我必须将 Javascript 对象转换为不可变序列,但这可能有效。
有没有其他人尝试解决这个问题,如果有,他们是如何解决这个问题的?
您可以制作原型并创建自己的列表,该列表的长度函数等于 returns 的大小。您还可以通过原型重写数组上的 forEach 方法以进行 break。
我能想到的唯一另一种不是超级干净的方法是在调用循环对象的函数并将其作为参数发送之前获取对象的长度或大小。
您可以使用 for...of
loop. This allows you operate on any Iterable 对象,其中包括内置集合以及 Immutable.js 集合。
for (let element of collection) {
// do something with element
if (someCondition) {
break;
}
}
它不会像常规 for
循环或 forEach
那样让您访问迭代索引,但这很容易跟踪您自己:
let i = 0;
for (let element of collection) {
// do something with element
if (someCondition) {
break;
}
i++;
}
您可能还想阅读 How to short circuit Array.forEach like calling break。它列出了几个解决方案,包括 for...of
应该适用于您要使用的所有集合:
- 在
try
中包装循环并抛出异常以中断 - 使用
.some()
和return true
打破