Fra*_*isc 507 javascript arrays
我现在用来检查这个功能如下:
function inArray(needle,haystack)
{
var count=haystack.length;
for(var i=0;i<count;i++)
{
if(haystack[i]===needle){return true;}
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
有用.我正在寻找的是,是否有更好的方法来做到这一点.
Ali*_*ter 874
ECMAScript 2016采用了includes()一种专门解决问题的阵列方法,因此现在是首选方法.
[1, 2, 3].includes(2); // true
[1, 2, 3].includes(4); // false
[1, 2, 3].includes(1, 2); // false (second parameter is the index position in this array at which to begin searching)
Run Code Online (Sandbox Code Playgroud)
截至2018年7月,几乎所有主流浏览器都已实现,如果您需要支持IE,则可以使用polyfill.
Ben*_*uer 416
码:
function isInArray(value, array) {
return array.indexOf(value) > -1;
}
Run Code Online (Sandbox Code Playgroud)
执行:
isInArray(1, [1,2,3]); // true
Run Code Online (Sandbox Code Playgroud)
更新(2017年):
在遵循ECMAScript 2016(ES7)标准的现代浏览器中,您可以使用函数Array.prototype.includes,这样可以更容易地检查数组中是否存在项:
const array = [1, 2, 3];
const value = 1;
const isInArray = array.includes(value);
console.log(isInArray); // trueRun Code Online (Sandbox Code Playgroud)
phi*_*hag 78
只需使用indexOf:
haystack.indexOf(needle) >= 0
Run Code Online (Sandbox Code Playgroud)
如果要支持旧的Internet Explorer(<IE9),则必须包含当前代码作为解决方法.
除非您的列表已排序,否则您需要将每个值与针进行比较.因此,您的解决方案indexOf都必须n/2平均执行比较.但是,由于它indexOf是一种内置方法,它可能会使用额外的优化,并且在实践中会稍微快一些.请注意,除非您的应用程序经常在列表中搜索(例如每秒1000次)或列表很大(比如100k条目),否则速度差异无关紧要.
psy*_*tor 33
我在Google Chrome 52上对它进行了多次基准测试,但可以随意将其复制到任何其他浏览器的控制台中.
var array = [0,1,2,3,4,5,6,7,8,9];
var result = 0;
var start = new Date().getTime();
for(var i = 0; i < 10000000; i++)
{
if(array.includes("test") === true){ result++; }
}
console.log(new Date().getTime() - start);
Run Code Online (Sandbox Code Playgroud)
var array = [0,1,2,3,4,5,6,7,8,9];
var result = 0;
var start = new Date().getTime();
for(var i = 0; i < 10000000; i++)
{
if(array.indexOf("test") > -1){ result++; }
}
console.log(new Date().getTime() - start);
Run Code Online (Sandbox Code Playgroud)
function inArray(target, array)
{
/* Caching array.length doesn't increase the performance of the for loop on V8 (and probably on most of other major engines) */
for(var i = 0; i < array.length; i++)
{
if(array[i] === target)
{
return true;
}
}
return false;
}
var array = [0,1,2,3,4,5,6,7,8,9];
var result = 0;
var start = new Date().getTime();
for(var i = 0; i < 10000000; i++)
{
if(inArray("test", array) === true){ result++; }
}
console.log(new Date().getTime() - start);
Run Code Online (Sandbox Code Playgroud)
Aka*_*ija 22
单行代码..将返回true或false
!!(arr.indexOf("val")+1)
Run Code Online (Sandbox Code Playgroud)
小智 15
您可以使用indexOf但在上一版本的Internet Explorer中运行不佳.
码:
function isInArray(value, array) {
return array.indexOf(value) > -1;
}
Run Code Online (Sandbox Code Playgroud)
执行:
isInArray(1, [1,2,3]); // true
Run Code Online (Sandbox Code Playgroud)
我建议你使用以下代码:
function inArray(needle, haystack) {
var length = haystack.length;
for (var i = 0; i < length; i++) {
if (haystack[i] == needle)
return true;
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
Nic*_*las 11
从 ECMAScript6 开始,可以使用 Set :
var myArray = ['A', 'B', 'C'];
var mySet = new Set(myArray);
var hasB = mySet.has('B'); // true
var hasZ = mySet.has('Z'); // false
Run Code Online (Sandbox Code Playgroud)
您可以使用underscore.js库中的_contains函数来实现此目的:
if (_.contains(haystack, needle)) {
console.log("Needle found.");
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
899110 次 |
| 最近记录: |