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 ...
随机推荐
- JAVA多线程之Synchronized、wait、notify实例讲解
一.Synchronized synchronized中文解释是同步,那么什么是同步呢,解释就是程序中用于控制不同线程间操作发生相对顺序的机制,通俗来讲就是2点,第一要有多线程,第二当多个线程同时竞争 ...
- 2、visual studio 常用设置
1.关闭 “引用” 提示 有时候感觉 “方法” 或者 “类” 上的引用有点乱: 去掉它的步骤: 1)在 “引用”文字上单击鼠标右键: 2)在弹出的“选项” 对话框中,取消 CodeLens: 2.打开 ...
- [转]James Bach:测试人员的角色
[转]James Bach:测试人员的角色 2015-05-13 以前,我是个开发人员.我不喜欢这个工作,无尽的压力让我疲惫.我几乎从未感觉到自己的工作做得足够好.我从未有过真正的休息.如果我没做好, ...
- jinja2问题集锦
用jinja2写模板的时候遇到了一些问题,记录一下 抽出base.html作为模板 之前的小项目写得都很不规范,模板都是能用就行,基本上只用到if语句,for语句和变量.导航栏都是复制粘贴,没有把共同 ...
- RCC—使用 HSE/HSI 配置时钟 ---时钟树
本章参考资料:< STM32F4xx 中文参考手册> RCC 章节.学习本章时,配合< STM32F4xx 中文参考手册> RCC 章节一起阅读,效果会更佳,特别是涉及到寄存器 ...
- [systemd]Linux系统启动之systemd
参照:https://wiki.debian.org/systemd 最近在添加板子应用程序自启动的时候,发现在rcN.d中的符号链接并没有用,文件系统为Debian Jessie 8, 后来从同事那 ...
- BlueZ--内核层+应用层
BlueZ 1.Kernel层实现: bluetooth协议栈有多层结构,最底层的硬件协议在硬件中就已经实现了.软件级别的协议实现,从HCI这一层开始实现. BlueZ对各层协议的实现是依托于Sock ...
- redis-stat 安装
apt-get install ruby apt-get install rubygems redis-stat安装: cd/root git clone https://github.com ...
- cacati的安装
一.安装rrdtool软件包 yum install -y cairo-devel libxml2-devel pango-devel pango libpng-devel freetype free ...
- LAMP 环境搭建关键步骤及注意事项
一.安装MySQL1): 编译安装MySQL+----------------------------------------------------------------------------- ...




