Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

79
Views
How to filter an array that contains functions

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.

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

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'));

about 4 years ago · Juan Pablo Isaza Report

0

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')();

about 4 years ago · Juan Pablo Isaza Report

0

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') );

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!