I have a javascript module and I import it at the beginning of my main / main code and in it I execute a forEach, in this main code there is a loop that calls the methods of the module:
results.forEach(function(result) {
for(const item of items .......){
exampleModule.metodo(result, item)
}
});
So far so good, the problem is that the values of the module are being mixed, the module is not dynamic according to the item Y of the result X, it kind of "disturbs" everything ...
I thought of a way to solve it, I do not know if it is the best practice, declare const exampleModule = require ('./modules/example.js') within an object and give that object a reference, such as item ID , since each ID will be unique:
let objetos = {}
results.forEach(function(result) {
for(const item of items .......){
objetos[item.id] = require('./modules/example.js')
objetos[item.id].metodo(result, item)
}
});
In short, I want each loop item to create an instance of the module without it being interfered with, if I instantiate the module in the item of position 0, everything in the module should only be used by that item 0, if it is item 1 the same thing, 1 n can change values of the module that is being used by position 0 and so on, is it possible? Can someone please help me?