İçin DöngüsüJavaScript for in deyimi bir Nesnenin özellikleri arasında döngü yapar:
for (key in object) {
// code block to be executed
}const person = {fname:"John", lname:"Doe", age:25};
let text = "";
for (let x in person) {
text += person[x];
}Kendiniz Deneyin →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript For In Loop</h2>
<p>The for in statement loops through the properties of an object:</p>
<p id="demo"></p>
<script>
const person = {fname:"John", lname:"Doe", age:25};
let txt = "";
for (let x in person) {
txt += person[x] + " ";
}
document.getElementById("demo").innerHTML = txt;
</script>
</body>
</html>
for in döngüsü bir person nesnesi üzerinde yinelenir
Her yineleme bir anahtar (x) döndürür
Anahtar, anahtarın değerine erişmek için kullanılır
Anahtarın değeri kişi[x]'dir
İçinde Diziler ÜzerindenJavaScript for in ifadesi aynı zamanda bir Dizinin özellikleri üzerinde de döngü oluşturabilir:
for (variable in array) {
code
}const numbers = [45, 4, 9, 16, 25];
let txt = "";
for (let x in numbers) {
txt += numbers[x];
}Kendiniz Deneyin →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Arrays</h1>
<h2>For In Loops</h2>
<p>The for in statement can loops over array values:</p>
<p id="demo"></p>
<script>
const numbers = [45, 4, 9, 16, 25];
let txt = "";
for (let x in numbers) {
txt += numbers[x] + "<br>";
}
document.getElementById("demo").innerHTML = txt;
</script>
</body>
</html>
Dizin sıralaması önemliyse, Dizi üzerinde for in öğesini kullanmayın.
Dizin sırası uygulamaya bağlıdır ve dizi değerlerine beklediğiniz sırayla erişilemeyebilir.
Sıra önemli olduğunda bir for döngüsü, bir for of döngüsü veya Array.forEach() kullanmak daha iyidir.
Array.forEach()forEach() yöntemi, her dizi öğesi için bir işlevi (geri çağırma işlevi) bir kez çağırır.
const numbers = [45, 4, 9, 16, 25];
let txt = "";
numbers.forEach(myFunction);
function myFunction(value, index, array) {
txt += value;
}Kendiniz Deneyin →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Arrays</h1>
<h2>The forEach() Method</h2>
<p>Call a function once for each array element:</p>
<p id="demo"></p>
<script>
const numbers = [45, 4, 9, 16, 25];
let txt = "";
numbers.forEach(myFunction);
document.getElementById("demo").innerHTML = txt;
function myFunction(value, index, array) {
txt += value + "<br>";
}
</script>
</body>
</html>
Fonksiyonun 3 argüman aldığını unutmayın:
Öğe değeri
Öğe dizini
Dizinin kendisi
Yukarıdaki örnekte yalnızca değer parametresi kullanılmaktadır. Şu şekilde yeniden yazılabilir:
const numbers = [45, 4, 9, 16, 25];
let txt = "";
numbers.forEach(myFunction);
function myFunction(value) {
txt += value;
}Kendiniz Deneyin →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Arrays</h1>
<h2>The forEach() Method</h2>
<p>Call a function once for each array element:</p>
<p id="demo"></p>
<script>
const numbers = [45, 4, 9, 16, 25];
let txt = "";
numbers.forEach(myFunction);
document.getElementById("demo").innerHTML = txt;
function myFunction(value) {
txt += value + "<br>";
}
</script>
</body>
</html>