I understand this is a very dumb question, I am dumb, maybe confused.
Let us say that I have a module foo.js
that after doing works returns an object like this bellow,
return {something, anotherThing, yetAnotherThing};
Now, I want to get those in my bar.js
and work with, but not with all of them. Maybe sometimes with something
and sometimes with anotherThing
etc. So, what I do is,
const foo = require('../foo.js')
const { something } = foo()
Then I work with something
in my files.
The problem here is, most of the time, I need something
or anotherThing
on a random basis. Like I do not know which one I will need. Sometimes I need all of them and somethings I need only one or two.
So, is it possible to write a function that dynamically gets the destructed variables and work with them? For example, if I create an array of the things needed from the foo
file and pass that array to a function that takes element from the array to get variable from foo.js
and work with it? An incorrect code example of what I am saying will be,
const iAmWrong = ( arr) => {
for (let i = 0; i < arr.length; i++) {
const { arr[i] } = foo();
}
arr[i]();
};
There must be a way to achieve this right? Maybe not how I am thinking it is in code but I hope it is not impossible.
Please help.