admin管理员组文章数量:1417547
I wasn't aware of the bad crossbrowser patibility of array.indexOf()
. But now that I am, I need to find a way to achieve the same thing but without using the previous method.
I tried googling for a while, but found no real convincing answers. For now, I am doing it with loops (but this is slow and I am sure there are better ways)
Side Notes:
- I can't use jQuery or any other libraries/frameworks.
- It doesn't necessarily need to return the index (a simply true/false will be ok)
I thought it is not necessary to share my code, since you all know how array-loop check looks like (plus it will lower your IQ)
I wasn't aware of the bad crossbrowser patibility of array.indexOf()
. But now that I am, I need to find a way to achieve the same thing but without using the previous method.
I tried googling for a while, but found no real convincing answers. For now, I am doing it with loops (but this is slow and I am sure there are better ways)
Side Notes:
- I can't use jQuery or any other libraries/frameworks.
- It doesn't necessarily need to return the index (a simply true/false will be ok)
I thought it is not necessary to share my code, since you all know how array-loop check looks like (plus it will lower your IQ)
Share Improve this question edited Sep 9, 2013 at 13:00 hakre 199k55 gold badges450 silver badges856 bronze badges asked Jan 13, 2012 at 20:04 mithril333221mithril333221 8192 gold badges10 silver badges20 bronze badges 3-
You could use the
indexOf
shim provided by Mozilla. – gen_Eric Commented Jan 13, 2012 at 20:07 -
You want to supply
IE lt 9
with ES5-shim... – Šime Vidas Commented Jan 13, 2012 at 20:10 -
Loops aren't bad at all. V8's
indexOf
also boils down to one. – pimvdb Commented Jan 13, 2012 at 20:31
3 Answers
Reset to default 6Here is how inArray is implemented in jQuery:
function inArray(elem, array, i) {
var len;
if ( array ) {
if ( array.indexOf ) {
return array.indexOf.call( array, elem, i );
}
len = array.length;
i = i ? i < 0 ? Math.max( 0, len + i ) : i : 0;
for ( ; i < len; i++ ) {
// Skip accessing in sparse arrays
if ( i in array && array[ i ] === elem ) {
return i;
}
}
}
return -1;
}
You can not use jQuery but why not use their implementation? :-)
Best regards!
From MDN:
if (!Array.prototype.indexOf) {
Array.prototype.indexOf = function (searchElement /*, fromIndex */ ) {
"use strict";
if (this == null) {
throw new TypeError();
}
var t = Object(this);
var len = t.length >>> 0;
if (len === 0) {
return -1;
}
var n = 0;
if (arguments.length > 0) {
n = Number(arguments[1]);
if (n != n) { // shortcut for verifying if it's NaN
n = 0;
} else if (n != 0 && n != Infinity && n != -Infinity) {
n = (n > 0 || -1) * Math.floor(Math.abs(n));
}
}
if (n >= len) {
return -1;
}
var k = n >= 0 ? n : Math.max(len - Math.abs(n), 0);
for (; k < len; k++) {
if (k in t && t[k] === searchElement) {
return k;
}
}
return -1;
}
}
This checks if it sees a native implementation, if not implement it.
Notable Quirks:
t.length >>> 0;
is an unsigned shift for force this to a positive number
For now, I am doing it with loops (but this is slow and I am sure there are better ways)
No matter what you do, it will at the end of the day involve loops. Unless you invent a O(1) algorithm for searching inside an array. There is nothing wrong with using a loop to find the corresponding element. You could even extend the built-in array object with this method so that you can reuse it.
本文标签: javascriptCrossbrowser quotinArrayquot function (without jQuery)Stack Overflow
版权声明:本文标题:javascript - Crossbrowser "inArray" function (without jQuery) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745272728a2650992.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论