Hi Peter
Danke für deinen Input zu meinem Problem. Leider übersteigen deine Beispiele meine JavaScript Kenntnisse, weshalb ich die Funktionen nicht an mein Script adaptieren soll; aber vorweg: es kann doch nicht sein, dass es so kompliziert ist, herauszufinden ob ein Wert in einem Array vorkommt oder nicht?? Das sollte doch mit einem simplen durchlaufen des Arrays, im Ansatz wie in meinem Post zu diesem Problem, zu lösen sein??
Gruss
IneX
[*1] Dein code erzwingt ueber "
return
" bei (i === 0)
den sofortigen ausstieg aus der schleife ...... persoenlich halte ich das fuer schlechten stil und
plaediere hiermit fuer den einsatz des stiefmuetterlich
behandelten "break
".unter beruecksichtigung moeglicher zukuenftiger array-
methoden koenntest Du Deine methode auch[indexOf]
nennen.
und damit diese nicht allein im globalen namensraum
abhaengen muss, darfst Du diese ruhig sowohl *statisch*
als auch *prototypisch* an den[[Array]]
-konstruktor tackern.siehe dazu auch:
[http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Objects:Array:indexOf]
hier ein moeglicher statischer entwurf ...
// [[link:http://www.pseliger.de/jsExtendedApi/jsApi.Array.mozGenerics.dev.js]]
//if (typeof Array.indexOf != "function") {
Array.indexOf = (function (obj, objLookingFor, idx) {
//var k, i = -1, l = ((typeof obj.length == "number") ? (obj.length) : (0));
var k, i = -1, l = (((obj instanceof Array) || ((typeof obj.length == "number") && ((typeof obj.item == "function") || (typeof obj.item == "object") || (typeof obj.item == "string") || (obj instanceof window.NodeList) || (obj instanceof window.HTMLCollection)))) ? (obj.length) : (0));//[[link:http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Objects:Array:indexOf#Parameters]]
idx = ((idx && isNaN(Number(idx))) ? (parseInt(Number(idx), 10)) : (0));
idx = ((idx < 0) ? (Math.max(0,(l + idx))) : (idx));for (k=idx; k<l; ++k) {
//if (obj[k] === objLookingFor) {
if ((obj[k] || obj.item(k)) === objLookingFor) {
i = k;
break;
}
}
return i;
});
//}
>
>
> ... und hier das prototypische gegenstück ...
>
>
> ~~~javascript
// [[link:http://www.pseliger.de/jsExtendedApi/jsApi.Array.mozExtensions.dev.js]]
> if (typeof Array.prototype.indexOf != "function") {
>
> Array.prototype.indexOf = (function (obj, idx) {
>
> //[http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Objects:Array:indexOf#Parameters]
> idx = ((idx && isNaN(Number(idx))) ? (parseInt(Number(idx), 10)) : (0));
> idx = ((idx < 0) ? (Math.max(0,(this.length + idx))) : (idx));
> var k, i = -1;
> for (k=idx; k<this.length; ++k) {
> if (this[k] === obj) {
> i = k;
> break;
> }
> }
> return i;
> });
> }
... darauf liessen sich dann auch noch die passenden eigenen
[contains]
-methoden pfropfen:
// [http://www.pseliger.de/jsExtendedApi/jsApi.Array.mozGenerics.dev.js]
//if (typeof Array.contains != "function") {
Array.contains = (function (obj, objLookingFor) {
return (Array.indexOf(obj, objLookingFor) >= 0);
});
//}
>
>
> ~~~javascript
// [http://www.pseliger.de/jsExtendedApi/jsApi.Array.mozExtensions.dev.js]
> if (typeof Array.prototype.contains != "function") {
>
> Array.prototype.contains = (function (obj) {
>
> return (this.indexOf(obj) >= 0);
> });
> }
so long - peterS. - pseliger@gmx.net