Javascript Array Iteration in Hindi – Array Iteration in Javascript in Hindi
इस Javascript Tutorial में Javascript Array Iteration in Hindi – Array Iteration in Javascript in Hindi के बारे में पढ़ेंगे।
Javascript Array Iteration in Hindi
Array Iteration का Use Array के प्रत्येक Element पे कुछ Operation Perform करने के लिए किया जाता है। Array Iteration Methods की पूरी List नीचे दी गयी है। जिनके बारे में अच्छे से समझाया भी गया है। आप इनको पूरी तरह ध्यान से पढ़े। क्युकी इनका Use Array Element पे Operation Perform करने के लिए किया जाता है।
forEarch() Method
forEach() Method के द्वारा प्रत्येक Array Element पे एक बार में Function Execute कर सकते है। इसके द्वारा प्रत्येक Element पे Function Call कर सकते है। इस Function में तीन Argument होते है।
- The item Value
- The item Index
- The array itself
<!DOCTYPE html>
<html>
<body>
<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>
45
4
9
16
25
map() Method
map() Method Array के सभी Element पे Perform करके एक New Array Create करता है। ये बिना Value के Array Element पे Perform नहीं करता है। ये Original Array में कोई भी Changing नहीं करता है। इसमें तीन Argument होते है।
- The Item Value
- The Item Index
- The Array Itself
<!DOCTYPE html>
<html>
<body>
<p id="map1"></p>
<script>
const numbers1 = [12, 8, 6, 16, 22];
const numbers2 = numbers1.map(myFunction);
document.getElementById("map1").innerHTML = numbers2;
function myFunction(value, index, array) {
return value * 5;
}
</script>
</body>
</html>
60,40,30,80,110
Reduce() Method
Reduce() method सभी Array Element पे एक Function Run करता है, और उन सभी को Single Value में show करा देता है। जैसे एक Example नीचे दे रहा हूँ। जो की सभी Array Element को Add के देता है। और Single Value में Result Show कराता है।
<!DOCTYPE html>
<html>
<body>
<p id="demo1"></p>
<script>
const numbers = [39, 2, 8, 15, 24];
let sum = numbers.reduce(myFunction);
document.getElementById("demo1").innerHTML = "The sum is " + sum;
function myFunction(total, value, index, array) {
return total + value;
}
</script>
</body>
</html>
The sum is 88
इसमें Total 4 Argument होते है।
- The Total Value (The Initial Value)
- The Item Value
- The Item Index
- The Array Itself
Filter() Method
इस Method के द्वारा Array में एक condition लगा सकते है। फिर Condition का जो भी Result है। वो हमें एक New Array के रूप में देता है।
<!DOCTYPE html>
<html>
<body>
<p id="vss"></p>
<script>
const numbers = [41, 4, 9, 13, 19];
const over18 = numbers.filter(myFunction);
document.getElementById("vss").innerHTML = over18;
function myFunction(value, index, array) {
return value > 18;
}
</script>
</body>
</html>
41,19
every() Method
ये method check करता है की जो Condition लगायी है। सभी Array Values उसको Pass कर रही है या नहीं।
<!DOCTYPE html>
<html>
<body>
<p id="vss"></p>
<script> const numbers = [41, 4, 9, 13, 19];
const over18 = numbers.every(myFunction);
document.getElementById("vss").innerHTML = over18;
function myFunction(value, index, array) {
return value > 18; }
</script>
</body>
</html>
false
यहाँ पे Condition Check हो रही है, की सभी Value 18 से बड़ी है। सभी Value 18 से बड़ी नहीं है, इसलिए false output दे रहा है।
Comments
Post a Comment