Skip to content
Advertisement

How to use each array value on each for cycle (Same quantity)

I have the following array called equipos_seleccionados

JavaScript

This is how the frontend looks:

enter image description here

As you can see, i automatically generate as many textbox as the value on inputs on the head section.

GOAL: I would like to use (on this case) the value 12 – v4 to the first 2 elements (Selects) and the value 100 – v500 for the next 4 elements (Selects).

This is the code i have so far:

JavaScript

This is how it should looks:

enter image description here

Advertisement

Answer

You can print just the element in equipos_seleccionados that is in the same position as result[“cantidad_seriado”] via the overload that uses the index of the forEach function.

You just need to change the .forEach(function(entry)) for .forEach(function(entry, index))

JavaScript
JavaScript

This works when both of your arrays come in the same order, in this case “2” is for “12 – v4” and “3” for “100 – v500”. If you don’t want to depend on the order of the items in both arrays you can use a dictionary based structure instead of that array, so you can have something like this:

JavaScript
JavaScript

That will ensure that your elements always match.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement