JavaScript Loop через массив
var colors = ["red","blue","green"];
colors.forEach(function(color) {
console.log(color);
});
Gifted Gerenuk
var colors = ["red","blue","green"];
colors.forEach(function(color) {
console.log(color);
});
var arr = [1, 2, 3, 4, 5];
arr.slice().reverse()
.forEach(function(item) {
console.log(item);
});
// 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)
for(i=0;i<=5;i++){
console.log(i);}
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
// 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
...