js for item in array code example

Example 1: es6 forEach

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

array1.forEach((element) => {
  console.log(element)
});

// expected output: "a"
// expected output: "b"
// expected output: "c"

Example 2: javascript object array iteration

let obj = {
  key1: "value1",
  key2: "value2",
  key3: "value3"
}

Object.keys(obj).forEach(key => {
  let value = obj[key];
  //use key and value here
});

Example 3: javascript foreach index

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

Example 4: js for each item do

let array = ['Item 1', 'Item 2', 'Item 3'];

array.forEach(item => {
	console.log(item); // Logs each 'Item #'
});

Example 5: for in javascript

for (let i = start; i < end; i++) {

}

Example 6: javascript iterate array

String[] myStringArray = {"Hello", "World"};
for (String s : myStringArray)
{
    // Do something
}

Tags:

Java Example