Answers for "loop on array"

32

javascript loop through array

var data = [1, 2, 3, 4, 5, 6];

// traditional for loop
for(let i=0; i<=data.length; i++) {
  console.log(data[i])  // 1 2 3 4 5 6
}

// using for...of
for(let i of data) {
	console.log(i) // 1 2 3 4 5 6
}

// using for...in
for(let i in data) {
  	console.log(i) // Prints indices for array elements
	console.log(data[i]) // 1 2 3 4 5 6
}

// using forEach
data.forEach((i) => {
  console.log(i) // 1 2 3 4 5 6
})
// NOTE ->  forEach method is about 95% slower than the traditional for loop

// using map
data.map((i) => {
  console.log(i) // 1 2 3 4 5 6
})
Posted by: Guest on December-18-2020
48

javascript code to loop through array

var colors = ["red","blue","green"];
for (var i = 0; i < colors.length; i++) {
    console.log(colors[i]);
}
Posted by: Guest on July-22-2019
13

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
});
Posted by: Guest on March-25-2020
-1

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]
Posted by: Guest on April-27-2021
-1

javascript array loop

const friends = [
   `Dale`,
   `Matt`,
   `Morne`,
   `Michael`,
];

for (let i = 0; i < friends.length; i++) {
   console.log(friends[i]);
}
Posted by: Guest on December-08-2020
0

javascript loop array

+-------------------------------+----------------+---------------------+---------------------+---------------------+--------+-------------+
|            Action             |   Condition    | AspectRatioDragging |      Dragging       |        Shape        | Simple |  Selected   |
+-------------------------------+----------------+---------------------+---------------------+---------------------+--------+-------------+
| Shift Released                |                | ?     ?     ?       |                     |                     |        |             |
| Shift Pressed                 |                |                     | AspectRatioDragging |                     |        |             |
| Primary Mouse Button Pressed  | Shift Key Held |                     |                     | ?    ?      ?       |        |             |
| Primary Mouse Button Pressed  |                |                     |                     | DraggingState       |        |             |
| Primary Mouse Button Released |  ?   ?         | SelectedState       | SelectedState       |                     |        |             |
| Primary Mouse Button Released |                | ShapeState          | ?    ?    ?         |                     |        |             |
| Escape Key                    |                | ?    ?   ?          | SimpleState         | SimpleState         |        | ?   ?   ?   |
| Ctrl + D & Clicking Outside   |                |                     |                     |                     |        | ?   ?   ?   |
+-------------------------------+----------------+---------------------+---------------------+---------------------+--------+-------------+
Posted by: Guest on June-26-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language