JavaScript では、 orステートメントlabel:
でジャンプ先として aを指定できます。そのため、に達すると、反復は、それが存在する内側のループではなく、外側の for ループに戻ります (これが のデフォルトの動作になります) 。continue
break
continue
continue
基本的に、この関数はnewArray
(古い配列を変更するのではなく) 新しい配列を作成し、元の配列のすべての要素をループすることによって機能します。newArray
まだ見つからない場合は、元の配列の要素を追加します。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 ドキュメント を参照してください。