“JS para bucles” Código de respuesta

JavaScript Loop a través de una matriz

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

bucle javascript

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

js bucle

// 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 para bucle [

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 para bucles

// 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

Respuestas similares a “JS para bucles”

Preguntas similares a “JS para bucles”

Más respuestas relacionadas con “JS para bucles” en JavaScript

Explore las respuestas de código populares por idioma

Explorar otros lenguajes de código