kul*_*hik 332 javascript ecmascript-5
我正在玩nodejs和mongoose - 尝试在深层注释中找到特定注释,并使用递归函数和foreach进行嵌套.有没有办法阻止nodejs forEach?据我所知,每个forEach迭代都是一个函数而且我不能只做"休息",只能"返回",但这不会阻止foreach.
function recurs(comment){
comment.comments.forEach(function(elem){
recurs(elem);
//if(...) break;
});
}
Run Code Online (Sandbox Code Playgroud)
sle*_*ica 844
你不能打破forEach.不过,我可以想到三种方法来伪造它.
1.丑陋的方式:传递给第二个参数forEach,以如上下文中使用,并且存储一个布尔在那里,然后使用if.这看起来很糟糕.
2.有争议的方式:将整个事物包围在一个try-catch区块中,并在想要破坏时抛出异常.这看起来很糟糕,可能会影响性能,但可以封装.
3.有趣的方式:使用 every().
['a', 'b', 'c'].every(function(element, index) {
// Do your thing, then:
if (you_want_to_break) return false
else return true
})
Run Code Online (Sandbox Code Playgroud)
some()相反,你可以使用,如果你想要return true打破.
Dom*_*nic 44
打破Array#forEach是不可能的.(您可以在链接页面上检查在Firefox中实现它的源代码,以确认这一点.)
相反,你应该使用一个正常的for循环:
function recurs(comment) {
for (var i = 0; i < comment.comments.length; ++i) {
var subComment = comment.comments[i];
recurs(subComment);
if (...) {
break;
}
}
}
Run Code Online (Sandbox Code Playgroud)
(或者,如果你想要更聪明一点,并且comment.comments[i]总是一个对象:)
function recurs(comment) {
for (var i = 0, subComment; subComment = comment.comments[i]; ++i) {
recurs(subComment);
if (...) {
break;
}
}
}
Run Code Online (Sandbox Code Playgroud)
Mar*_*ahn 29
正如其他人指出的那样,你无法取消forEach循环,但这是我的解决方案:
ary.forEach(function loop(){
if(loop.stop){ return; }
if(condition){ loop.stop = true; }
});
Run Code Online (Sandbox Code Playgroud)
当然这实际上并没有打破循环,它只是阻止了"break"之后所有元素的代码执行
Shr*_*ani 15
我想你想使用Array.prototype.发现 查找时,它在阵列中找到您的具体价值将打破本身.
var inventory = [
{name: 'apples', quantity: 2},
{name: 'bananas', quantity: 0},
{name: 'cherries', quantity: 5}
];
function findCherries(fruit) {
return fruit.name === 'cherries';
}
console.log(inventory.find(findCherries));
// { name: 'cherries', quantity: 5 }
Run Code Online (Sandbox Code Playgroud)
forEach不会在返回时破坏,有很难实现这项工作的解决方案,但我建议不要使用它,而是尝试使用Array.prototype.some或Array.prototype.every
var ar = [1,2,3,4,5];
ar.some(function(item,index){
if(item == 3){
return true;
}
console.log("item is :"+item+" index is : "+index);
});Run Code Online (Sandbox Code Playgroud)
var f = "how to stop Javascript forEach?".split(' ');
f.forEach(function (a,b){
console.info(b+1);
if (a == 'stop') {
console.warn("\tposition: \'stop\'["+(b+1)+"] \r\n\tall length: " + (f.length));
f.length = 0; //<--!!!
}
});
Run Code Online (Sandbox Code Playgroud)
forEach如果您不介意使用第三方库,可以使用Lodash的功能.
例:
var _ = require('lodash');
_.forEach(comments, function (comment) {
do_something_with(comment);
if (...) {
return false; // Exits the loop.
}
})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
400314 次 |
| 最近记录: |