我如何在箭头函数中定义这段代码,我想使用迭代数组元素?

How can i define this peice of code inside arrow function, i want to use the iterating arrayElement?

我希望函数能够输出 true 或 false,如果 searchElement 存在于数组中它应该 return true 或 false。我知道我可以使用简单的 for 循环和 if-else 组合,但我想使用 arrow function.

我知道默认情况下 'includes' 函数存在于 js 中,我正试图制作一个类似于它的函数,这就是我不想使用它的原因

const somestuff=[1,2,54,23,65,132,76,'A','wth','Health is Wealth'];
function includes(array,searchElement){

    const result=somestuff.filter(arrayElement=> arrayElement===searchElement)
    if(arrayElement===searchElement){
        console.log('true');
    }
    else{
        console.log('false');
    }
}

includes(somestuff,65);

这一行单独为我提供了 arrayElement,我希望它 return true 或 false,这就是为什么我尝试了 if else 语句但我不知道如何在箭头功能线,我认为 === 应该 returned true 或 false 而不是数字本身,请告诉我我做错了什么,谢谢

const result=somestuff.filter(arrayElement=> arrayElement===searchElement)

这可以用查找函数而不是过滤器来实现。

现场演示:https://replit.com/@kallefrombosnia/PalatableStingySphere#index.js

// Define function
const includes = (array, element) =>{
  return array.find(item => item === element) ? true : false;
}

// Log output in console
console.log(includes([1, 2, 3], 4));

const includes = (array, element) =>{
  return array.find(item => item === element) ? true : false;
}

console.log(includes([1, 2, 3], 4));

此问题的另一个解决方案是使用 Array prototype.

中的 some 方法
const includes = (array, pattern) => {
    const comp_function = (element) => element === pattern
    return array.some(comp_function)
}

k = [1, 2, 3, 'a', 'b', 'c']

includes(k, 1) // true
includes(k, 'a') // true
includes(k, '1') // false
includes(k, 'd') // false

为什么你必须实施“包括”,

我们有 Array.prototype.includes() 规范,所以我们可以做类似

的事情

const array1 = [1, 2, 3];

array1.includes(2) // return true

并详细了解规范 here

您可以使用 Array.prototype.find()。我解释过滤器是从中删除并排列一些具有相同模式的元素,但是您可以使用 map() 或 find() 是使我们的生活更轻松的 ES6 功能。本demo将为您讲解

// 地图()

const result=somestuff.find(arrayElement=>  { 
if(arrayElement===searchElement){
        console.log('true');
return arrayElement
    }
    else{
        console.log('false');
    })
})

// 查找()

const result=somestuff.find(arrayElement=> arrayElement===searchElement)

终于,我们得到了这个

const somestuff=[1,2,54,23,65,132,76,'A','wth','Health is Wealth'];
function includes(array,searchElement){

   const result=somestuff.find(arrayElement=> arrayElement===searchElement)
   return result
}

includes(somestuff,65);

这就是我在你们的帮助下解决问题的方法

我需要变量 searchElement 所在位置的索引以及它是否存在。

const exists = (element) => element === searchElement;
    console.log(' exists at index: '+somestuff.findIndex(exists) +' therefore: '+somestuff.some(exists));

意味着无论哪个值满足要求都会存储在现有变量中,现在我可以对它们应用数组方法,例如 findIndex() 和 some()

findIndex() 说明值的索引,即使有一个值满足要求,some() 也会 return 为真

虽然这是我最初想做的:

const somestuff=[1,2,54,23,65,132,76,'A','wth','Health is Wealth'];
const searchElement=54;
const result=somestuff.find(arrayElement=>  { 
    
    if(arrayElement===searchElement){
        console.log('true '+arrayElement);
    console.log(somestuff.indexOf(searchElement));}
    });