VBScript array_map function

A VBScript equivalent of PHP’s array_map


Function array_map(callback, arr)

    Dim key
    Dim tmp_ar

    If isArray( arr ) Then

        If Len( callback ) = 0 Then
            array_map = arr
            Exit Function
        End If

        ReDim tmp_ar( uBound(arr) )
        For key = 0 to uBound( arr )
            If isObject( arr(key) ) Then
                execute("set tmp_ar(key) = " & callback & "(arr(key))")
            Else
                execute("tmp_ar(key) = " & callback & "(arr(key))")
            End If
        Next

        array_map = tmp_ar

    ElseIf isObject( arr ) Then

        If Len( callback ) = 0 Then
            set array_map = arr
            Exit Function
        End If

        Dim return_val

        set tmp_ar = Server.CreateObject("Scripting.Dictionary")
        For Each key In arr
            return_val = "
            If isObject( arr.Item(key) ) Then
                execute("set return_val = " & callback & "(arr.Item(key))")
            Else
                execute("return_val = " & callback & "(arr.Item(key))")
            End If
            tmp_ar.Add key, return_val
        Next

        set array_map = tmp_ar

    End If

End Function

Please also note that php.vbs offers community built functions and goes by the McDonald’s Theory. We’ll put online functions that are far from perfect, in the hopes to spark better contributions. Do you have one? Then please just:

Other PHP functions in the array extension