find-if-an-item-is-in-a-javascript-array
http://stackoverflow.com/questions/143847/best-way-to-find-if-an-item-is-in-a-javascript-array
Best way to find if an item is in a JavaScript array? [duplicate]
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:
|
|||||||||||||||||
marked as duplicate by Gothdojavascript Jan 6 at 21:04This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question. |
|||||||||||||||||
|
EDIT: This will not work on IE6, 7 or 8 though. The best workaround is to define it yourself if it's not present:
|
|||||||||||||||||
|

If you are using jQuery:
For more information: http://api.jquery.com/jQuery.inArray/ |
|||||
|
First, implement
It's changed to store the length so that it doesn't need to look it up every iteration. But the difference isn't huge. A less general purpose function might be faster:
I prefer using the standard function and leaving this sort of micro-optimization for when it's really needed. But if you're keen on micro-optimization I adapted the benchmarks that roosterononacid linked to in the comments, to benchmark searching in arrays. They're pretty crude though, a full investigation would test arrays with different types, different lengths and finding objects that occur in different places. |
|||||||||||||||||
|
If the array is unsorted, there isn't really a better way (aside from using the above-mentioned indexOf, which I think amounts to the same thing). If the array is sorted, you can do a binary search, which works like this:
Binary search runs in time proportional to the logarithm of the length of the array, so it can be much faster than looking at each individual element. |
|||||||||||||||||
|
assuming .indexOf() is implemented
!!! do not make
the use of 2nd arg (flag) forces comparation by value instead of reference |
||||
It depends on your purpose. If you program for the Web, avoid
|
|||||||||||||
|
A robust way to check if an object is an array in javascript is detailed here: Here are two functions from the xa.js framework which I attach to a
If you then want to check if an object is in an array, I would also include this code:
And finally this in_array function:
|
|||||||||||||
|
Here's some meta-knowledge for you - if you want to know what you can do with an Array, check the documentation - here's the Array page for Mozilla https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array There you'll see reference to indexOf, added in Javascript 1.6 |
|||||||||||||
|
find-if-an-item-is-in-a-javascript-array的更多相关文章
- javascript array操作
首先来看一下怎么判断一个对象是不是数组: 1.Array.isArray(obj) 调用数组的isArray方法 2.obj instanceof Array 判断对象是否是Array的实例 3.Ob ...
- JavaScript Array -->map()、filter()、reduce()、forEach()函数的使用
题目: 1.得到 3000 到 3500 之内工资的人. 2.增加一个年龄的字段,并且计算其年龄. 3.打印出每个人的所在城市 4.计算所有人的工资的总和. 测试数据: function getDat ...
- JavaScript Array methods performance compare
JavaScript Array methods performance compare JavaScript数组方法的性能对比 env $ node -v # v12.18.0 push vs un ...
- 最全总结 JavaScript Array 方法详解
JavaScript Array 指南.png Array API 大全 (公众号: 前端自学社区).png 前言 我们在日常开发中,与接口打交道最多了,前端通过访问后端接口,然后将接口数据二次处理渲 ...
- JavaScript Array 对象
JavaScript Array 对象 Array 对象 Array 对象用于在变量中存储多个值: var cars = ["Saab", "Volvo", & ...
- JavaScript Array(数组)对象
一,定义数组 数组对象用来在单独的变量名中存储一系列的值. 创建 Array 对象的语法: new Array(); new Array(size); new Array(element0, elem ...
- Javascript Array.prototype.some()
当我们使用数组时,查找数组中包含某个特殊的项是非常常见的动作.下面例子是一个简单的实现: 01 planets = [ 02 "mercury", 03 " ...
- [Javascript ] Array methods in depth - sort
Sort can automatically arrange items in an array. In this lesson we look at the basics including how ...
- Javascript Array 方法整理
Javascript Array 方法整理 Javascript 数组相关方法 说明 大多数其它编程语言不允许改变数组大小,越界访问索引会报错,但是 javascript不会报错,不过不建议直接修改a ...
- JavaScript : Array assignment creates reference not copy
JavaScript : Array assignment creates reference not copy 29 May 2015 Consider we have an array var a ...
随机推荐
- cnblogs博客迁移到hexo
cnblogs博客备份 备份地址:https://i.cnblogs.com/BlogBackup.aspx?type=1 备份文件为xml格式,打开备份文件,如下所示: <?xml versi ...
- vue中config/index.js:配置的详细理解
当我们需要和后台分离部署的时候,必须配置config/index.js: 用vue-cli 自动构建的目录里面 (环境变量及其基本变量的配置) var path = require('path') ...
- Oracle:shared memory realm does not exist
1. 先描述一个连接Oracle 10g的错误:“shared memory realm does not exist” 如图所示Sqlplus连接时出现这个错误: 2. Oracle 服务器主要组件 ...
- ewebeditor编辑器配合IIS6.0解析漏洞拿shell
很明显这是一个ewebeditor编辑器,这个编辑器存在可遍历目录可创建文件夹等一系列漏洞.直接在url处加../即可.若要创建文件夹直接在url后面写文件夹名称即可. 上传一张shell图片,抓包改 ...
- STM32F10x_硬件I2C主从通信(轮询发送,中断接收)
Ⅰ.写在前面 关注我分享文章的朋友应该知道我在前面讲述过(软件.硬件)I2C主机控制从机EEPROM的例子.在I2C通信主机控制程序是比较常见的一种,可以说在实际项目中,很多应用都会使用到I2C通信. ...
- python 神经网络实例
#http://python.jobbole.com/82758/ # import numpy as np # # # # sigmoid function # def nonlin(x, deri ...
- 001servlet的基本知识
servlet的知识: l 1. servlet概念及相关接口简介 l 2. servet 执行过程 l 3. servlet路径映射 l 4. 缺省servlet --应用 ...
- thymeleaf教程
本教程涵盖了常见的前端操作,比如,判断,循环,引入模板,常用函数(日期格式化,字符串操作)下拉,js和css中使用,基本可以应对一般场景. 怎么使用? 前端html页面标签中引入如下: <htm ...
- 在函数体的“出口处”,对 return 语句的正确性和效率进行检查
在函数体的“出口处”,对 return 语句的正确性和效率进行检查. 如果函数有返回值,那么函数的“出口处”是 return 语句. 我们不要轻视 return 语 句.如果 return 语句写得不 ...
- final可以修饰类、属性、方法
final可以修饰类.属性.方法. 当用final修饰类的时候,此类不可被继承,即final类没有子类.这样可以用final保证用户调用时动作的一致性,可以防止子类覆盖情况的发生. 当利用final修 ...