Best way to find if an item is in a JavaScript array? [duplicate]

問題:

This question already has answers here : 這個問題在這裏已經有了答案
Closed 4 years ago . 4年前關閉。

What is the best way to find if an object is in an array?查找對象是否在數組中的最佳方法是什麼?

This is the best way I know:這是我所知道的最好的方法:

function include(arr, obj) { for (var i = 0; i < arr.length; i++) { if (arr[i] == obj) return true; } } console.log(include([1, 2, 3, 4], 3)); // true console.log(include([1, 2, 3, 4], 6)); // undefined


解決方案:

參考一: https://en.stackoom.com/question/bQ7
參考二: https://stackoom.com/question/bQ7
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章