Skip to content

Commit 61ea4ca

Browse files
committed
Reduce duplicate code
do not duplicate the array walk, just check in the same for both cases
1 parent 26c41aa commit 61ea4ca

File tree

1 file changed

+3
-12
lines changed

1 file changed

+3
-12
lines changed

functions/array/in_array.js

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ function in_array(needle, haystack, argStrict) {
22
// discuss at: http://phpjs.org/functions/in_array/
33
// original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
44
// improved by: vlado houba
5+
// improved by: Jonas Sciangula Street (Joni2Back)
56
// input by: Billy
67
// bugfixed by: Brett Zamir (http://brett-zamir.me)
78
// example 1: in_array('van', ['Kevin', 'van', 'Zonneveld']);
@@ -17,20 +18,10 @@ function in_array(needle, haystack, argStrict) {
1718

1819
var key = '',
1920
strict = !! argStrict;
20-
21-
if (strict) {
2221
for (key in haystack) {
23-
if (haystack[key] === needle) {
22+
if ((strict && haystack[key] === needle) || (!strict && haystack[key] == needle)) {
2423
return true;
2524
}
26-
}
27-
} else {
28-
for (key in haystack) {
29-
if (haystack[key] == needle) {
30-
return true;
31-
}
32-
}
3325
}
34-
3526
return false;
36-
}
27+
}

0 commit comments

Comments
 (0)