Q:

foreach js

let words = ['one', 'two', 'three', 'four'];
words.forEach((word) => {
  console.log(word);
});
// one
// two
// three
// four
21
var items = ["item1", "item2", "item3"]
var copie = [];

items.forEach(function(item){
  copie.push(item);
});
11
var stringArray = ["first", "second"];

myArray.forEach((string, index) => {
  	var msg = "The string: " + string + " is in index of " + index; 
	console.log(msg);
	
	// Output:
	// The string: first is in index of 0
	// The string: second is in index of 1
});
0
let colors = ['red', 'blue', 'green'];
// idx and sourceArr optional; sourceArr == colors
colors.forEach(function(color, idx, sourceArr) {
	console.log(color, idx, sourceArr)
});
// Output:
// red 0 ['red', 'blue', 'green']
// blue 1 ['red', 'blue', 'green']
// green 2 ['red', 'blue', 'green']
7
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']
8
///Simple One
var a = ["a", "b", "c"];
a.forEach(function(entry) {
    console.log(entry);
});


///Function  concept

var fruits = ["apple", "orange", "cherry"];
fruits.forEach(myFunction);

function myFunction(item, index) {
  document.getElementById("demo").innerHTML += index + ":" + item + "<br>";
}
0
var a = ["a", "b", "c"];
a.forEach(function(entry) {
    console.log(entry);
});
3
// 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);
1
Used to execute the same code on every element in an array
Does not change the array
Returns undefined
2
arr.forEach(callback(currentValue [, index [, array]])[, thisArg]);
0

New to Communities?

Join the community