{"record":{"id":"357a85f21484be69","repo":"karatelabs/karate","slug":"map-needs-two-arguments-list-and-function","errorCode":null,"errorMessage":"map() needs two arguments: list and function","messagePattern":"map\\(\\) 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":328,"sourceCode":"            } else if (obj instanceof Map) {\n                // Convert to JSON string, lowercase, parse back\n                String json = Json.stringifyStrict(obj).toLowerCase();\n                return Json.of(json).value();\n            } else if (obj instanceof List) {\n                String json = Json.stringifyStrict(obj).toLowerCase();\n                return Json.of(json).value();\n            } else if (obj instanceof Node) {\n                String xml = Xml.toString((Node) obj, false).toLowerCase();\n                return Xml.toXmlDoc(xml);\n            }\n            return obj;\n        };\n    }\n\n    static JavaInvokable map() {\n        return args -> {\n            if (args.length < 2) {\n                throw new RuntimeException(\"map() 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                result.add(fn.call(null, new Object[]{list.get(i), i}));\n            }\n            return result;\n        };\n    }\n\n    static JavaInvokable mapWithKey() {\n        return args -> {\n            if (args.length < 2) {\n                throw new RuntimeException(\"mapWithKey() needs two arguments: list and key name\");\n            }\n            Object listArg = args[0];\n            if (listArg == null) {","sourceCodeStart":310,"sourceCodeEnd":346,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-core/src/main/java/io/karatelabs/core/KarateJsUtils.java#L310-L346","documentation":"map() is a Karate JS utility that applies a function to each element of a List (the function receives element and index) and returns a new List. It throws this error when called with fewer than two arguments, because both the source list and the transform function are mandatory.","triggerScenarios":"karate.map() with zero or one argument, e.g. map(myList) without the function, or map() entirely; passing null/undefined so only one effective argument is bound.","commonSituations":"Forgetting the second (function) argument when converting from a for-loop; passing the function in the wrong order; a variable holding the function is undefined so the arity check trips.","solutions":["Supply both arguments: map(myList, fn) where fn(item, index) returns the transformed value.","Ensure the second argument is actually a function/lamba (e.g. function(x, i){ return x.name }), not a string or value.","Check the list variable is defined; if it may be null, return an empty list instead of calling map()."],"exampleFix":"// before\ndef names = map(users)\n// after\ndef names = map(users, function(u, i){ return u.name })","handlingStrategy":"validation","validationCode":"def out = (list && fn) ? map(list, fn) : []","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Remember the signature: map(list, function(item, index){...}) — two args always.","Define the transform function before the map() call.","Prefer list comprehension-style helpers only after verifying the list is not null."],"tags":["karate","javascript","argument-count","lists"],"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-19T12:17:13.211Z"}