get array index in foreach javascript code example

Example 1: javascript foreach index

users.forEach((user, index)=>{
	console.log(index); // Prints the index at which the loop is currently at
});

Example 2: forEach index

const array1 = ['a', 'b', 'c'];

array1.forEach((element, index) => console.log(element, index));

Example 3: foreach loop javascript

listName.forEach((listItem) => {
  Logger.log(listItem);
}):

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

Tags:

Ruby Example