Home > Enterprise >  How to filter an array that contains functions
How to filter an array that contains functions

Time:10-27

so like I'm required to write if/else statement in shorthand or in functions instead of :
if (hero === "Robin"){return callRobin()}... or instead of switch cases.

const callRobin = () => `Hey Robin`;
const callRaven = () => `Hey Raven`;
const callStarFire = () => `Hey StarFire`;
const callBeastBoy = () => `Hey BeastBoy`;

// these were the functions!!

const herosFuncArr = [callRobin, callRaven, callStarFire, callBeastBoy];  //an array that contains the functions
const herosStringsArr = ['Robin', 'Raven', 'StarFire', 'BeastBoy'];
const myFunc = param => param == herosStringsArr.filter(x => x.includes(param)) ? herosFuncArr.filter(z => z.name.includes(param)()) : false;
myFunc('StarFire');

my point in this code was : when we inter a hero name as a parameter, if it exists in the strings array, return an element from the functions array that has the same letters the parameter have AS A FUNCTION as indicated with the double parentheses.

I tried so many things, also tried eval (`call${param}()) but apparently that's unacceptable. also tried .toString but didn't work (for me). any help would be appreciated.

CodePudding user response:

You're far better off eliminating all of those functions, and creating one callHero function into which you can pass the name of the found hero and return a string. You don't need to worry about filter; use find to find the first match.

And it's best not to use includes because that will match Star to StarFire which you probably don't want to do. Just do a simple comparison instead.

const heroes = ['Robin', 'Raven', 'StarFire', 'BeastBoy'];

// Return a string
function callHero(hero) {
  return `Hey ${hero}!`;
}

function isAHero(name) {

  // Find the hero in the array
  const hero = heroes.find(hero => hero === name);

  // If it exists call the `callHero` function with the hero name
  // and return the resulting string from the function
  if (hero) return callHero(hero);

  // Otherwise return something else
  return `Boo! ${name} is not a hero.`;
}

console.log(isAHero('Robin'));
console.log(isAHero('Billy Joel'));
console.log(isAHero('StarFire'));
console.log(isAHero('Star'));
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

This might work for you. Find the index of given name in herosStringsArr, then get the function in herosFuncArr at index

const callRobin = () => `Hey Robin`;
const callRaven = () => `Hey Raven`;
const callStarFire = () => `Hey StarFire`;
const callBeastBoy = () => `Hey BeastBoy`;

// these were the functions!!

const herosFuncArr = [callRobin, callRaven, callStarFire, callBeastBoy];  //an array that contains the functions
const herosStringsArr = ['Robin', 'Raven', 'StarFire', 'BeastBoy'];
const myFunc = param => {
const index = herosStringsArr.findIndex(el => el === param);
if (index >= 0) {
    return herosFuncArr[index];
} else {
    return undefined;
}
};
const func = myFunc('StarFire');
if (typeof func === 'function') {
console.log(func.toString());
console.log(func.call());
}
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

You can call the functions and compare the returned string:

const callRobin = () => `Hey Robin`;
const callRaven = () => `Hey Raven`;
const callStarFire = () => `Hey StarFire`;
const callBeastBoy = () => `Hey BeastBoy`;

// these were the functions!!

const herosFuncArr = [callRobin, callRaven, callStarFire, callBeastBoy];  //an array that contains the functions
const herosStringsArr = ['Robin', 'Raven', 'StarFire', 'BeastBoy'];
const myFunc = param => param == herosStringsArr.filter(x => x.includes(param)) ? herosFuncArr.filter(z => z().includes(param)) : false;

console.log(myFunc('StarFire'));
<iframe name="sif3" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

You can do it with the help of Map. You should place heroStringsArr as key and your functions as value:

const map = new Map();
map.set('Robin', () => console.log('Hey Robin'));
map.set('Raven', () => console.log('Hey Raven'));
map.set('StarFire', () => console.log('Hey StarFire'));
map.set('BeastBoy', () => console.log('Hey BeastBoy'));

const myFunc = param => map.has(param) ? map.get(param) : console.log('This function is not available!');

myFunc('Robin')();
<iframe name="sif4" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

I like @Andy's idea of just one function. Also if you must have many functions, essentially multiple "instances" of the same function, you can start of with an array of strings which you can then map using the one function to an array of functions.

You can as well use regular expressions as in the demo below:

const callRobin = () => `Hey Robin`;
const callRaven = () => `Hey Raven`;
const callStarFire = () => `Hey StarFire`;
const callBeastBoy = () => `Hey BeastBoy`;

// these were the functions!!

const herosFuncArr = [callRobin, callRaven, callStarFire, callBeastBoy];  //an array that contains the functions
const herosStringsArr = ['Robin', 'Raven', 'StarFire', 'BeastBoy'];
const myFunc = param => herosFuncArr.filter(f => (new RegExp(`\\b${param}\\b`)).test( f() )).length > 0; 
console.log( myFunc('StarFire') );
<iframe name="sif5" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related