8
Warum gibt lodash -1 hier zurück? Es ist klar drin?Lodash findIndex funktioniert nicht
Ignores = ['load', 'test', 'ok'];
alert(_.findIndex(Ignores, 'ok'));
Warum gibt lodash -1 hier zurück? Es ist klar drin?Lodash findIndex funktioniert nicht
Ignores = ['load', 'test', 'ok'];
alert(_.findIndex(Ignores, 'ok'));
Das ist, weil findIndex() nimmt als eine Array-Parameter und ein Prädikat, eine Funktion, die einen booleschen Wert auf einer Bedingung basiert zurückgibt.
Angenommen, Sie für needle
in haystack
suchen, können Sie erreichen, was Sie mit normalen JavaScript wollen:
alert(haystack.indexOf(needle));
Sie _.indexOf
verwenden können (von @Juhana):
alert(_.indexOf(haystack, needle))
Sie können tun, es mit _.findIndex
auch:
alert(_.findIndex(haystack, function(x) { return x === needle; }));
oder:
alert(_.findIndex(haystack, _(needle).isEqual));
@yeouuu Wenn Sie * wirklich * verwenden möchten lodash gibt es [ '_.indexOf()'] (https://lodash.com/docs#indexOf), die dasselbe tut wie die native Methode (wörtlich, wenn die JS-Engine dies unterstützt) und mit weniger Overhead als '_.findIndex()'. – JJJ
@Juhana Oh, das ist nett. Das habe ich nicht bemerkt. Ich füge das zur Antwort hinzu. – tgkokk