for loop array script code example
Example 1: loop from array bash
#!/bin/bash
array=( one two three )
for i in "${array[@]}"
do
echo $i
done
Example 2: loop through an array javascript
let array = ['Item 1', 'Item 2', 'Item 3'];
// Here's 4 different ways
for (let index = 0; index < array.length; index++) {
console.log(array[index]);
}
for (let index in array) {
console.log(array[index]);
}
for (let value of array) {
console.log(value); // Will log value in array
}
array.forEach((value, index) => {
console.log(index); // Will log each index
console.log(value); // Will log each value
});
Example 3: javascript loop array
var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
for( var i = 0; i < arr.length; i--){
if ( arr[i] === 5) {
arr.splice(i, 1);
}
}
//=> [1, 2, 3, 4, 6, 7, 8, 9, 0]