
(This builds on a previous discussion I had started looking to prioritize one argument in the list supplied by the user.) I am trying to expand my understanding of fat arrow syntax and the spread operator, but I'm having trouble using them together. How do I refer to the spread elements of an array within the function to which they are sent? Example... given the chat command line of: !the_script_name --foo:bar --qud:zu --qud:the dangles --q:third --port:out --starboard:home --posh --notes:Best Practices: vol. 1 ...I create an array of arguments ( args ) in the form of: [["foo","bar"],["qud","zu"],["qud","the dangles"],["q","third"],["port","out"],["starboard","home"],["posh",""],["notes","Best Practices: vol. 1"]] If I want to copy out the elements from this array that match the argument "qud" (the first element of the inner arrays), I can do this: let matchedArgs = []; matchedArgs.push(...args.filter(lookFor("qud"))); // this works But if I had a bunch of arguments that I wanted to pull out (more than just "qud"), I would need to pass each through that call to lookFor . If I wanted to pull out both the "qud" arguments and the "q" arguments, I would have an array ( argAliases ) in the form of: ["q","qud"] This starts to sound like a case for the spread syntax, only I'm not sure how to reference the element from the argAliases array within the function that I'm sending it to. Here is what I am trying: (...argAliases) => (alias) => { matchedArgs.push(...args.filter(lookFor(alias))); }; I read that as "take each element of argAliases and pass it as ' alias ' into the lookFor() function, which will be used as the basis of a filter on the args array; push each result into the matchedArgs array." It's basically taking my statement that works (for just the static value "qud") and trying to turn it into a fat arrow function to which I can pass each element of argAliases , but I'm just not having any luck. Sandbox and Full Code This is in the form of an HTML file to mimic the chat so I don't have to constantly break and restart my sandbox. The functionality should be the same. If there is still some question about why I'm trying to find an answer to this specific problem, I can explain more about the logic behind it. <!DOCTYPE html>
<html>
<body>
Command Line Supplied: <input type="text" id="msg" value="!the_script_name --foo:bar --qud:zu --qud:the dangles --q:third --port:out --starboard:home --posh --notes:Best Practices: vol. 1" style="width:400px;">
<button onclick="handleInput()">Test It</button>
<p id="log"></p>
<script>
function log(stmt) {
document.getElementById("log").innerHTML += JSON.stringify(stmt) + '<br><br>';
return;
};
const splitArgs = (a) => { return a.split(":") };
const joinVals = (a) => { return [a.slice(0)[0], a.slice(1).join(":").trim()]; };
const lookFor = (arg) => (a) => { return a[0] === arg; };
const lookForVal = (arg) => (a) => { return a[1] === arg; };
const getKeyByValue = (object, value) => {
let matchArr = Object.entries(object)
.filter(lookForVal(value))
.map((a) => { return a[0]; });
return matchArr;
};
const argAliasTable = {
f:"foo",
foo:"foo",
b: "bar",
bar: "bar",
ba: "baz",
baz: "baz",
q: "qud",
qud: "qud",
};
const handleInput = () => {
document.getElementById("log").innerHTML = "";
let msg = {};
msg.content = document.getElementById("msg").value;
let args = msg.content.split(/\s--/)
.slice(1)
.map(splitArgs)
.map(joinVals);
log(args);
processQud(args);
log(args);
return;
};
const processQud = (args) => {
let argAliases = getKeyByValue(argAliasTable,"qud");
log(`===== argAliases =====`);
log(argAliases);
let matchedArgs = [];
matchedArgs.push(...args.filter(lookFor("qud")));
// (...argAliases) => (alias) => { matchedArgs.push(...args.filter(lookFor(alias))); };
log(`===== matchedArgs =====`);
log(matchedArgs);
if (matchedArgs.length) {
log(`This is where the stuff happens.`);
// remove each element from args array
}
return;
};
</script>
</body>
</html>