簡體   English   中英

如何在箭頭函數中定義這段代碼,我想使用迭代 arrayElement?

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

我希望函數能夠輸出真或假,如果數組中存在 searchElement,它應該返回真或假。 我知道我可以使用簡單的 for 循環和 if-else 組合,但我想使用箭頭函數

我知道默認情況下 '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,我希望它返回 true 或 false,這就是為什么我嘗試了 if else 語句,但我不知道如何在箭頭函數行中包含該代碼塊,我認為 == = 應該返回 true 或 false 而不是數字本身,請讓我知道我做錯了什么,謝謝

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

這可以用 find 函數而不是過濾器來實現。

現場演示: 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 原型中的一些方法。

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

並在此處了解有關規范的更多信息

您可以使用 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() 也會返回 true

這就是我最初想做的事情:

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


暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM