“JS pour les boucles” Réponses codées

boucle javascript via le tableau

var colors = ["red","blue","green"];
colors.forEach(function(color) {
  console.log(color);
});
Gifted Gerenuk

boucle javascript

var arr = [1, 2, 3, 4, 5];
 
arr.slice().reverse()
    .forEach(function(item) {
            console.log(item);
        });
Friendly Fox

boucle js

// Arrow function
forEach((element) => { ... } )
forEach((element, index) => { ... } )
forEach((element, index, array) => { ... } )

// Callback function
forEach(callbackFn)
forEach(callbackFn, thisArg)

// Inline callback function
forEach(function callbackFn(element) { ... })
forEach(function callbackFn(element, index) { ... })
forEach(function callbackFn(element, index, array){ ... })
forEach(function callbackFn(element, index, array) { ... }, thisArg)
Charming Civet

JavaScript pour Loop [

for(i=0;i<=5;i++){
  console.log(i);}
Angry Aardvark

javascript.loop

echo "# AashutoshSINHA" >> README.md
git init
git add README.md
git commit -m "first commit"
git branch -M main
git remote add origin https://github.com/AashutoshSINHA/AashutoshSINHA.git
git push -u origin main
Mysterious Mink

JS pour les boucles

// Sample Data:
const days = ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"]

// Samle Data which has undefined items:
const days = ["Monday","Tuesday",,"Thursday",,"Saturday","Sunday"]

// This Post might be a good read, to see a few caveats and advantages:
// https://stackoverflow.com/questions/500504/why-is-using-for-in-for-array-iteration-a-bad-idea

//Classic For Loop using 3 expressions iterates all array indexes:
for (let i=0; i<days.length; i++){
    console.log("Day: "+days[i])
}

//for..in iterates defined array indexes:
for (let day in days){
    console.log("Day: ", days[day]);
}

//for..of iterates all array items:
for (let day of days){
    console.log("Day: ", day);
}

//using array methods might be more suitable depending on the scenario:
days.forEach(function(day){
    console.log("Day: ", day);
});
//available methods:
days.forEach()
days.map()
days.filter()
days.reduce()
days.every()
days.some() //this one is neat imo
...
Tender Tern

Réponses similaires à “JS pour les boucles”

Questions similaires à “JS pour les boucles”

Plus de réponses similaires à “JS pour les boucles” dans JavaScript

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code