javascript foreach
const avengers = ['thor', 'captain america', 'hulk'];
avengers.forEach((item, index)=>{
console.log(index, item)
})
javascript foreach
const avengers = ['thor', 'captain america', 'hulk'];
avengers.forEach((item, index)=>{
console.log(index, item)
})
foreach javascript
let words = ['one', 'two', 'three', 'four'];
words.forEach((word) => {
console.log(word);
});
// one
// two
// three
// four
foreach javascript
var items = ["item1", "item2", "item3"]
var copie = [];
items.forEach(function(item){
copie.push(item);
});
foreach javascript
let names = ['josh', 'joe', 'ben', 'dylan'];
// index and sourceArr are optional, sourceArr == ['josh', 'joe', 'ben', 'dylan']
names.forEach((name, index, sourceArr) => {
console.log(color, idx, sourceArr)
});
// josh 0 ['josh', 'joe', 'ben', 'dylan']
// joe 1 ['josh', 'joe', 'ben', 'dylan']
// ben 2 ['josh', 'joe', 'ben', 'dylan']
// dylan 3 ['josh', 'joe', 'ben', 'dylan']
string foreach javascript
//for
str='hello'
for (var i = 0; i < str.length; i++) {
console.info(str[i]);
}
//for...of
let result1 = '';
for (let letter of str) {
result1 += letter;
}
console.log(`for...of : ${result1}`)
//forEach
// ES6 version.
let result2 = '';
str.split('').forEach(letter => {
result2 += letter;
});
console.log(`forEach ES6 : ${result2}`)
//or
var result3 = '';
str.split('').forEach(function(letter) {
result3 += letter;
});
console.log(`forEach standard : ${result3}`)
//for...in
var result4 = '';
for (var letterIndex in str) {
result4 += str[letterIndex];
}
console.log(`for...in : ${result4}`)
//map
// ES6 version.
var result5 = '';
str.split('').map(letter => {
result5 += letter;
});
console.log(`map ES6 : ${result5}`)
//or
let result6 = '';
str.split('').map(function(letter) {
result6 += letter;
});
console.log(`map standard : ${result6}`)
foreach javascript
let colors = ['red', 'blue', 'green'];
// idx and sourceArr optional; sourceArr == colors
colors.forEach(function(color, idx, sourceArr) {
console.log(color, idx, sourceArr)
});
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us