0
Good morning guys, I have a javascript module and I import it at the beginning of my main/main code and I run a foreach, still in this main code has 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 item Y of the result X, it kind of "mess" all over...
I thought of a way to solve, I don’t know if it’s best practice, declare const exampleModule = require('./modules/example.js')
within an object and give that object a reference, such as the 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 them being interfered with, if I instantiate the module in position item 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?
"the module is not dynamic according to the Y item of the X result" This has become quite confusing and gives the impression that what you are doing can be a XY problem. Why do you need a module to be "dynamic"? And what would those Y and X values be?
– Woss
Describing the general problem you will get only a general answer. Example: "How to build a house?" , "Use blocks and cement, build solid walls". Instead ask something specific and responsive in a useful way: "How to lift a wall using this type of block with such a slope and such height safely?" , answer: "Position the blocks in such format, run this block placement algorithm, do not use this tool because there is such a risk, here is an example running from a wall ready for you to see how it does [link]". See? Too many wide questions don’t help.
– Sorack
I’m sorry, I didn’t know how to express myself in the best way. Is that this problem never happened to me, X and Y are loop positions, the item and the result are objects come from the API and are used in the modules
– viniciussvl