當前位置:成語大全網 - 新華字典 - js filter find 使用及區別

js filter find 使用及區別

相同點:使用語法相同? 均不改變原數組

array.filter(function(value, index, arr),thisValue)

array.find(function(value, index, arr),thisValue)

value:必須? 代表當前元素 ? index:可選? 當前元素索引?

arr:可選 當前數組 thisValue:傳遞給函數的值,壹般用this值,為空時undfined傳給this

不同點:find 從數組中查找符合條件的第壹個元素並返回。filter從數組中查找符合條件的所有元素並返回數組。

舉例:

所以,當查找數組中符合條件的所有元素時用filter,查找數組中符合條件的第壹個元素用find。