foreach js example

Example 1: javascript foreach

const avengers = ['thor', 'captain america', 'hulk'];
avengers.forEach((item, index)=>{
	console.log(index, item)
})

Example 2: javascript foreach example

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

Example 3: how to use the foreach method in javascript

groceries.forEach(groceryItem => console.log(groceryItem));

Example 4: foreach js

// result.params.detail = {
// "status": true,
// "message": "",
// "params": {
//     "pay_credit_remain": 115,
//     "month_expiry": "15",
//     "detail": [
//         {
//             "credit": "70",
//             "create_date": "2020-10-16",
//             "expiry_date": "2022-01-16"
//         },
//         {
//             "credit": "45",
//             "create_date": "2020-10-17",
//             "expiry_date": "2022-01-17"
//         }
//     ]
// }
//}

                 
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);

Example 5: foreach javascript

arr.forEach(callback(currentValue [, index [, array]])[, thisArg]);