查找项目是否在JavaScript数组中的最佳方法? [重复]

走远了吗. 提交于 2020-08-16 19:08:22

问题:

This question already has an answer here: 这个问题已经在这里有了答案:

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

include([1,2,3,4], 3); // true
include([1,2,3,4], 6); // undefined

解决方案:

参考一: https://stackoom.com/question/bQ7/查找项目是否在JavaScript数组中的最佳方法-重复
参考二: https://oldbug.net/q/bQ7/Best-way-to-find-if-an-item-is-in-a-JavaScript-array-duplicate
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!