js each array code example
Example 1: javascript foreach
var colors = ['red', 'blue', 'green'];
colors.forEach(function(color) {
console.log(color);
});
Example 2: js for each item in array
const array1 = ['a', 'b', 'c'];
array1.forEach(element => console.log(element));
Example 3: javascript foreach index
users.forEach((user, index)=>{
console.log(index);
});
Example 4: foreach loop javascript
const numList = [1, 2, 3];
numList.forEach((number) => {
console.log(number);
});
Example 5: for each array javascript
var fruits = ["apple", "orange", "cherry"];
fruits.forEach(getArrayValues);
function getArrayValues(item, index) {
console.log( index + ":" + item);
}
Example 6: foreach js
detail = "<ul>";
result.params.detail.forEach((val) => {
detail = detail + sprintf(lang.pay_credit_detail, val.credit, val.expiry_date);
});
detail += "</ul>";
$('#pay_credit_remain').html(detail);