{"record":{"id":"e9449f32b8091170","repo":"karatelabs/karate","slug":"filter-needs-two-arguments-list-and-function","errorCode":null,"errorMessage":"filter() needs two arguments: list and function","messagePattern":"filter\\(\\) needs two arguments: list and function","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"karate-core/src/main/java/io/karatelabs/core/KarateJsUtils.java","lineNumber":183,"sourceCode":"                throw new RuntimeException(\"extractAll() needs three arguments: text, regex, group\");\n            }\n            String text = args[0].toString();\n            String regex = args[1].toString();\n            int group = ((Number) args[2]).intValue();\n            Pattern pattern = Pattern.compile(regex);\n            Matcher matcher = pattern.matcher(text);\n            List<String> list = new ArrayList<>();\n            while (matcher.find()) {\n                list.add(matcher.group(group));\n            }\n            return list;\n        };\n    }\n\n    static JavaInvokable filter() {\n        return args -> {\n            if (args.length < 2) {\n                throw new RuntimeException(\"filter() needs two arguments: list and function\");\n            }\n            List<?> list = (List<?>) args[0];\n            JavaCallable fn = (JavaCallable) args[1];\n            List<Object> result = new ArrayList<>();\n            for (int i = 0; i < list.size(); i++) {\n                Object item = list.get(i);\n                Object keep = fn.call(null, new Object[]{item, i});\n                if (Boolean.TRUE.equals(keep)) {\n                    result.add(item);\n                }\n            }\n            return result;\n        };\n    }\n\n    @SuppressWarnings(\"unchecked\")\n    static JavaInvokable filterKeys() {\n        return args -> {","sourceCodeStart":165,"sourceCodeEnd":201,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-core/src/main/java/io/karatelabs/core/KarateJsUtils.java#L165-L201","documentation":"Karate's filter() JS utility builds a new list containing items for which a predicate function returns truthy. It requires two arguments: the source list and a callable predicate; fewer arguments throw this error.","triggerScenarios":"Calling filter(list) without the function, filter() with nothing, or passing a non-callable (string/JS object) as the second argument so the guard on arg count (or the cast) fails.","commonSituations":"Passing a native Array.prototype.filter-style callback string; forgetting the arrow function after refactoring; confusing filter with filterKeys which takes a map/keys instead of a function.","solutions":["Pass a JS function as the second argument: filter(list, function(x){ return x.active })","Check that the first argument is actually a list","If filtering a map by keys, use filterKeys() instead"],"exampleFix":"// before\nvar active = karate.filter(users);\n// after\nvar active = karate.filter(users, u => u.active);","handlingStrategy":"type-guard","validationCode":"if (!Array.isArray(list) || typeof fn !== 'function') throw new Error('filter requires a list and a function');","typeGuard":"function canFilter(args) { return args.length >= 2 && Array.isArray(args[0]) && typeof args[1] === 'function'; }","tryCatchPattern":"try { out = karate.filter(list, fn); } catch (e) { karate.log('filter failed: ' + e.message); }","preventionTips":["Always pass a JS function as the predicate","Use filterKeys() for maps, filter() for lists","Check arg arrays for undefined before invoking helpers"],"tags":["argument-count","callback","javascript","karate"],"backgroundTag":"missing-required-argument","analyzedSha":"a22eb90246d958d15a47bf436693d0121ad2812d","analyzedAt":"2026-09-12T09:01:00.220Z","contentChangedAt":"2026-09-12T09:01:00.220Z","schemaVersion":2},"datasetVersion":"2026-09-16T19:17:19.609Z"}