如何过滤包含函数的数组



就像我需要用速记或函数来写if/else语句一样,而不是:
if (hero === "Robin"){return callRobin()}…或代替开关箱

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');
我在这段代码中的观点是:当我们将英雄的名字作为参数插入时,如果它存在于字符串数组中,那么从函数数组中返回一个与参数具有相同字母的元素作为FUNCTION,用双括号表示。

我尝试了很多东西,也尝试了eval (' call${param}()),但显然这是不可接受的。也尝试过。tostring,但没有工作(对我来说)。如有任何帮助,不胜感激。

最好消除所有这些函数,并创建一个callHero函数,您可以将找到的英雄的名称传递给该函数并返回一个字符串。你不需要担心filter;使用find查找第一个匹配

最好不要使用includes,因为这将使StarStarFire匹配,这可能是您不想做的。只需做一个简单的比较。

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

您可以调用函数并比较返回的字符串:

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

您可以在Map的帮助下完成。你应该把heroStringsArr作为键,把你的函数作为值:

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

我喜欢@Andy关于只有一个函数的想法。此外,如果你必须有很多函数,本质上是多个"实例"对于同一个函数,可以从字符串数组开始,然后使用一个函数将字符串数组映射到函数数组。

您也可以使用正则表达式,如下面的示例所示:

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

相关内容

  • 没有找到相关文章

最新更新