I'm really not sure how to word this question, which is why I've had some trouble googling it, but I'm trying to make, basically, a helper function for modifying mongoDB data, and I have a list of commands, in this format:
var commands = {
"find": {
"names": {
"find":["query"],
"toArray":["callback"]
}
},
"sort": {
"names": {
"find":[],
"sort":["query"],
"toArray":["callback"]
}
},
"limit": {
"names": {
"find":[],
"limit":["number"],
"toArray":["callback"]
}
},
"deleteOne": {
"args":["query","callback"]
},
"findOne": {
"args":["query", "callback"]
},
"insertOne": {
"args":["query", "callback"]
},
"insertMany": {
"args":["array"]
},
"remove": {
"args":["query", "callback"]
},
"drop": {
"args":["callback"]
},
"updateOne": {
"args":["query", "newvalues", "callback"]
},
"aggregate": {
"args": ["query"],
"hasCB":"toArray"
},
"createCollection": {
"args":["string", "callback"]
}
};
and then a dictionary of those values: and the idea is to call a particular mongoDB function based on the input, like
Based off of this, I am able to make an array that looks like this:
[["find",[]],["sort",[{"name":-1}]],["toArray",[/*some function reference*/]]],
and based on that, I want to call:
something.find().sort({"name":-1}).toArray(callback);
and if I have something like this:
[["find",[{"name":"hi"}]],["toArray",[/some function reference/]]],
I should be able to produce the result of: something.find({"name":"hi"}).toArray(cb);
all in the same function.
This isn't EXACTLY a node.js question, more of JavaScript in general, that given a function in the format of
[[functionName1,arrayOfArguments1],[functionName2,arrayOfArguments2],[functionName3,arrayOfArguments3]]
how do you call
functionName1(...arrayOfArguments1)[functionName2](...arrayOfArguments2)[functionName3](...arrayOfArguments3)
,
using a loop, or one function ,lets say? Instead of manually writing it out, how do I produce this result? I'm currently doing it like this with a switch / case, but that's less than ideal:
switch(funcList.length) {
case 2:
console.log(col[funcList[0][0]](...funcList[0][1])[funcList[1][0]](...funcList[1][1]));
break;
case 3:
console.log(col[funcList[0][0]](...funcList[0][1])[funcList[1][0]](...funcList[1][1])[funcList[2][0]](...funcList[2][1]));
break;
}