Given an array of named functions, how can I delete all but a specified one? The functions are exported e.g. exports.foo. To mock an example:
var test = [];
test.push(foo = function(){});
test.push(bar = function(){});
console.log(test);
Logs:
[ [Function], [Function] ]
How can I delete all functions except a specified one e.g. bar?
EDIT: Some additional context - I have a set of plugins (functions) that get loaded automatically in to an array. As part of my Mocha tests, I wish to test each one individually, hence wanting to drop all but a specified function.
I am going to question why you'd want to do this, but if you're set on doing it, this is how I'd approach it:
function deleteAllButThis(fn, arr){
for(var i=0; i < arr.length; i++) {
//if fn in array does NOT match fn passed in,
if (fn !== arr[i]) {
arr.splice(i, 1); //remove current array item
}
}
return arr;
}
And then you'd call it like this:
deleteAllButThis(foo, test);