diff --git a/js/modules/k6/html/html.go b/js/modules/k6/html/html.go
index 4720d3ec5e5..1ad1b9deaf3 100644
--- a/js/modules/k6/html/html.go
+++ b/js/modules/k6/html/html.go
@@ -426,21 +426,22 @@ func (s Selection) Is(v goja.Value) bool {
}
// Map implements ES5 Array.prototype.map
-func (s Selection) Map(v goja.Value) []string {
+func (s Selection) Map(v goja.Value) []goja.Value {
gojaFn, isFn := goja.AssertFunction(v)
if !isFn {
common.Throw(s.rt, errors.New("the argument to map() must be a function"))
}
- fn := func(idx int, sel *goquery.Selection) string {
+ var values []goja.Value
+ s.sel.Each(func(idx int, sel *goquery.Selection) {
selection := &Selection{sel: sel, URL: s.URL, rt: s.rt}
+
if fnRes, fnErr := gojaFn(v, s.rt.ToValue(idx), s.rt.ToValue(selection)); fnErr == nil {
- return fnRes.String()
+ values = append(values, fnRes)
}
- return ""
- }
+ })
- return s.sel.Map(fn)
+ return values
}
func (s Selection) Slice(start int, def ...int) Selection {
diff --git a/js/modules/k6/html/html_test.go b/js/modules/k6/html/html_test.go
index 6fec3cbe685..43064f55ee7 100644
--- a/js/modules/k6/html/html_test.go
+++ b/js/modules/k6/html/html_test.go
@@ -66,6 +66,25 @@ const testHTML = `