Perform actions for each element of a Javascript array [closed]

0

I do not mean literally the element itself, but rather its number. That is, suppose the following array:

var disparos = {

           colorBala: this.color;
           potenciaBala: this.pB;
           mL: this.mL;

        }

    var b = []; // Este array recibe valores segun la cantidad de disparos

    var b = [disparos,  disparos]; // Suponiendo que disparó 2 balas:

Assuming that

  

shots

     

represents a shot and has its properties, colors, functions   , etc .

Then I want it if it has fired 1 time, so: var b = [disparos];

a canvas is created, the problem is that with forEach it realizes the element itself, as if it changed its properties, etc, I only want it to act according to the number of elements, < strong> iterating for each shot that can be tens and so for each shot create a canvas drawing.

    
asked by Eduardo Sebastian 04.05.2017 в 23:10
source

1 answer

1

If the elements contained in the array do not matter since there will be an action for each contained element, what matters is the number of elements contained in the array, you just have to obtain the size of the array, iterate that number of times , launching the action for each iteration.

Example:

var fruits = ["Banana", "Orange", "Apple", "Mango"];
var n = fruits.length;

for ( var i = 0; i < n; i++ ) {
    console.log( "Lanzada la acción: " + ( i + 1 ));
}

Result:

Lanzada la acción: 1
Lanzada la acción: 2
Lanzada la acción: 3
Lanzada la acción: 4
    
answered by 05.05.2017 / 00:29
source