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 ...
随机推荐
- vue-router介绍
vue-router学习 转自:https://my.oschina.net/u/1416844/blog/849971 1. vue-router介绍 vue-router把react-router ...
- ASP.NET基础(一)
ExecuteNonQuery()的用法 下面我们将详细讲解如何在Page_Load()中对数据库的增加.删除.修改,最后我们再来总结一下ExecuteNonQuery(),ExecuteScalar ...
- 微博excel数据清洗(Java版)
微博数据清洗(Java版) 原创 2013年12月10日 10:58:24 2979 大数据公益大学提供的一份数据,义务处理一下,原始数据是Excel,含有html标签,如下: 要求清洗掉html ...
- 未能加载文件或程序集“Autofac, Version=3.4.0.0,
遇到这个错误的时候:如下图 未能加载文件或程序集“Autofac, Version=3.4.0.0, Culture=neutral, PublicKeyToken=17863af14b0044da” ...
- jquery checkbox勾选/取消
作者:@keenleung本文为作者原创,转载请注明出处:http://www.cnblogs.com/KeenLeung/p/3799895.html 目录 <form> 你爱好的运动是 ...
- 在项目中使用JMail发送邮件
1.添加JMail组件:在vs的解决方案资源管理器中,右键添加引用,选择浏览,选择jmail.dll文件,在Bin文件夹中就可以看到添加的JMail组件. 2.注册JMail组件:开始-->运行 ...
- 【BZOJ】1492: [NOI2007]货币兑换Cash(cdq分治)
http://www.lydsy.com/JudgeOnline/problem.php?id=1492 蒟蒻来学学cdq神算法啊.. 详见论文 陈丹琦<从<Cash>谈一类分治算法 ...
- ThinkPHP项目笔记之登录,注册,安全退出篇
1.先说注册 a.准备好注册页面,register.html,当然一般有,姓名,邮箱,地址等常用的. b."不要相信用户提交的一切数据",安全,安全是第一位的.所以要做判断,客户端 ...
- Mock利器:PowerMock
powerMock不仅支持接口mock,final类.静态类.静态方法.私有方法都支持mock,还是很强大的: 1.gradle引用: myonlycompile('org.powermock:pow ...
- Redis分布式锁,基于StringRedisTemplate和基于Lettuce实现setNx
使用redis分布式锁,来确保多个服务对共享数据操作的唯一性一般来说有StringRedisTemplate和RedisTemplate两种redis操作模板. 根据key-value的类型决定使用哪 ...




