在 JavaScript 中,您可以在or语句中将 a指定label:
为要跳转到的位置。因此,当达到时,迭代将返回到外部 for 循环,而不是它所在的内部循环(这将是 的默认行为)continue
break
continue
continue
基本上,此函数通过创建一个新数组newArray
(不修改旧数组)并遍历原始数组中的每个元素来工作。它将原始数组中的元素添加到newArray
if not already found 中。newArray
它通过在旧数组循环的每次迭代中循环它并查找匹配值来确定它是否已经存在arrayName[i]
function removeDuplicateElement(arrayName)
{
// Declares a new array to hold the deduped values
var newArray=new Array();
// Loops over the original array
// label: here defines a point for the continue statement to target
label:for(var i=0; i<arrayName.length;i++ )
{
// Loops over the new array to see if the current value from the old array
// already exists here
for(var j=0; j<newArray.length;j++ )
{
// The new array already has the current loop val from the old array
if(newArray[j]==arrayName[i])
// So it returns to the outer loop taking no further action
// This advances the outer loop to its next iteration
continue label;
}
// Otherwise, the current value is added to the new array
newArray[newArray.length] = arrayName[i];
}
// The new deduped array is returned from the function
return newArray;
}
有关continue
此上下文中的功能的更多信息,请查看 MDN 文档。