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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章