jQuery 跳过嵌套 .each 中父循环的迭代
jQuery skip an iteration of the parent loop in nested .each
我有以下构造:
注意:本例中的数组和目标只是象征性的!我知道这个例子会有更好的解决方案 - 它只是为了演示代码的构造。
var firstArray = [1,2,3,4,5];
var secondArray = [1,2,4,5];
$.each(firstArray,function(i,firstArrayElement){
$.each(secondArray,function(i,secondArrayElement){
if(firstArrayElement === secondArrayElement) {
// do stuff
// PROBLEM: force the firstArray loop to continue with the next iteration
}
});
console.log("Didn't find: "+firstArrayElement);
});
澄清我的问题:有没有办法强制父 .each 在下一次迭代中继续(=跳过 console.log
)?在 PHP 中,这将是 continue 2;
。所以目标是,如果条件为真(在这个例子中找到一个元素),永远不会达到 console.log()
。
有 the flag solution (jsFiddle),但必须有更好的方法来做到这一点。
还有 solutions with labels 但标签不适用于 .each()。
所以我正在寻找的可能是一种在没有标志的父函数中使用 return
的方法。对于上面的示例,这意味着结果记录:Didn't find 3
编辑(就像其他人已经建议的那样):
$.each(firstArray,function(i,firstArrayElement){
var idx = $.inArray(firstArray[i], secondArray);
if(idx == -1) {
console.log("Didn't find: "+firstArrayElement);
}
else{
//to do...
}
});
不知道是否符合你的问题...
解法:
在评论中找到解释。
$.each(firstArray,function(i,firstArrayElement){
//Initialize skipParent at each Iteration
var skipParent = false;
$.each(secondArray,function(i,secondArrayElement){
if(firstArrayElement === secondArrayElement) {
// do stuff
//set skipParent to true
skipParent = true;
}
});
//if skipParent true, we continue to next iteration.
if(skipParent)
{
console.log("Didn't find: "+firstArrayElement);
return false;
}
});
所以,这有点 hacky...但它确实实现了在没有标志的情况下将 true 返回给父函数的目标:
var firstArray = [1,2,3,4,5];
var secondArray = [1,2,4,5];
$.each(firstArray, function(i, firstArrayElement) {
try
{
$.each(secondArray, function(i, secondArrayElement) {
if (firstArrayElement === secondArrayElement) {
throw 'Found';
}
});
} catch(err) {
return true;
}
console.log("Didn't find: "+firstArrayElement);
});
$.each
方法的响应始终是被迭代的对象。如果我们能控制它,就会有一个选择。我们不能,您将无法逃脱标志解决方案。
如果您愿意放弃 $.each
,至少在内部循环中使用 for
,这应该可以完成工作:
var firstArray = [1, 2, 3, 4, 5],
secondArray = [1, 2, 4, 5];
$.each(firstArray,function(i, firstArrayElement) {
var x = null;
for (var j = 0; j < secondArray.length; j++) {
var secondArrayElement = secondArray[j];
if (firstArrayElement === secondArrayElement) {
console.log('Do stuff');
// return false here if you wan't to break after the first match.
} else if (j + 1 == secondArray.length) {
return false;
}
}
console.log('Didn\'t find: ' + firstArrayElement);
});
我有以下构造:
注意:本例中的数组和目标只是象征性的!我知道这个例子会有更好的解决方案 - 它只是为了演示代码的构造。
var firstArray = [1,2,3,4,5];
var secondArray = [1,2,4,5];
$.each(firstArray,function(i,firstArrayElement){
$.each(secondArray,function(i,secondArrayElement){
if(firstArrayElement === secondArrayElement) {
// do stuff
// PROBLEM: force the firstArray loop to continue with the next iteration
}
});
console.log("Didn't find: "+firstArrayElement);
});
澄清我的问题:有没有办法强制父 .each 在下一次迭代中继续(=跳过 console.log
)?在 PHP 中,这将是 continue 2;
。所以目标是,如果条件为真(在这个例子中找到一个元素),永远不会达到 console.log()
。
有 the flag solution (jsFiddle),但必须有更好的方法来做到这一点。
还有 solutions with labels 但标签不适用于 .each()。
所以我正在寻找的可能是一种在没有标志的父函数中使用 return
的方法。对于上面的示例,这意味着结果记录:Didn't find 3
编辑(就像其他人已经建议的那样):
$.each(firstArray,function(i,firstArrayElement){
var idx = $.inArray(firstArray[i], secondArray);
if(idx == -1) {
console.log("Didn't find: "+firstArrayElement);
}
else{
//to do...
}
});
不知道是否符合你的问题...
解法:
在评论中找到解释。
$.each(firstArray,function(i,firstArrayElement){
//Initialize skipParent at each Iteration
var skipParent = false;
$.each(secondArray,function(i,secondArrayElement){
if(firstArrayElement === secondArrayElement) {
// do stuff
//set skipParent to true
skipParent = true;
}
});
//if skipParent true, we continue to next iteration.
if(skipParent)
{
console.log("Didn't find: "+firstArrayElement);
return false;
}
});
所以,这有点 hacky...但它确实实现了在没有标志的情况下将 true 返回给父函数的目标:
var firstArray = [1,2,3,4,5];
var secondArray = [1,2,4,5];
$.each(firstArray, function(i, firstArrayElement) {
try
{
$.each(secondArray, function(i, secondArrayElement) {
if (firstArrayElement === secondArrayElement) {
throw 'Found';
}
});
} catch(err) {
return true;
}
console.log("Didn't find: "+firstArrayElement);
});
$.each
方法的响应始终是被迭代的对象。如果我们能控制它,就会有一个选择。我们不能,您将无法逃脱标志解决方案。
如果您愿意放弃 $.each
,至少在内部循环中使用 for
,这应该可以完成工作:
var firstArray = [1, 2, 3, 4, 5],
secondArray = [1, 2, 4, 5];
$.each(firstArray,function(i, firstArrayElement) {
var x = null;
for (var j = 0; j < secondArray.length; j++) {
var secondArrayElement = secondArray[j];
if (firstArrayElement === secondArrayElement) {
console.log('Do stuff');
// return false here if you wan't to break after the first match.
} else if (j + 1 == secondArray.length) {
return false;
}
}
console.log('Didn\'t find: ' + firstArrayElement);
});