I have an array of objects(can be of any length):
[
{
"operator": "sortBy",
"value": "price"
},
{
"operator": "startAt",
"value": "7"
},
...
]
where operator is a name of a function that can be imported from the JS module:
{
sortBy: f(),
startAt: f(),
...
}
and value is any value with a type of string or number.
How can I create functions from this array and pass them as arguments to another function?
For example:
import * as Utils from "@externalPackage";
const filters = [
{
"operator": "sortBy",
"value": "price"
},
{
"operator": "startAt",
"value": "7"
}
]
const anotherFunction = (arg1, arg2, ...) => ...
//this function can have any number of arguments
const helperFunction = (arr)=>//What should I do here with filters array to get a list of callable arguments(=>Utils.sortBy("price"), Utils.startAt(7)) ?//
const result = anotherFunction(helperFunction(filters))
^ ^ ^
| | |
//This should be transformed in anotherFunction(Utils.sortBy("price"), Utils.startAt(7))//
Utils.sortBy("price"), you can simply translate that to a map call.const helperFunction = (arr)=> arr.map(({operator, value})=> (x) => Utils[operator](value)). But it's unclear how the Util functions are being called or what is being passed to them. Voting to close for clarity.sortBy: f()should besortBy: f. You don't want to call the function when creating the object, you just want to store a reference to the function in the object.