2

I'm trying to write a function that will run encodeURIComponent on arrays or objects regardless of the depth. It all seems to go well until I return the final result.

    <script src="https://code.jquery.com/jquery-3.1.0.min.js" integrity="sha256-cCueBR6CsyA4/9szpPfrX3s49M9vUU5BgtiJj06wt/s=" crossorigin="anonymous"></script>
<script>
function encodeURI_array($Array) {
    $Result = [];
    if($Array !== null && typeof $Array === 'object') {
        //for(var key in $Array) {
        Object.keys($Array).forEach(function (key) {
            if($Array[key] !== null && typeof $Array[key] === 'object') {
                console.log("encode array:  " + key + " : " + $Array[key]);
                $Result[key] = encodeURI_array($Array[key]);
            } else { 
                $Result[key] = encodeURIComponent($Array[key],"UTF-8").replace(/\+/g, ' ').replace("%26","&").replace("%2C",",");
                console.log("encode strings:  " + key + " : " + $Result[key]);
            }
        });
        console.log("Final result");
        console.log($Result);
        return $Result;
    } else {
        $Result = encodeURIComponent($Array,"UTF-8").replace(/\+/g, ' ').replace("%26","&").replace("%2C",",");
        console.log("encode string: " + $Result);
        return $Result;
    }
}

$TestArray = [{"Key1":"Value1"},{"Key2":"Value2"},{"Key3":"Value3"}];
$TestArray2 = {"Key":"Value"};
(encodeURI_array($TestArray));
//console.log(encodeURI_array($TestArray2));
</script>

For this script, the very last result returns only the last object in the array when it should be returning an array of objects.

Luke Pring
  • 992
  • 3
  • 11
  • 16
  • Cant you just use JSON? – Alex K. Sep 20 '16 at 11:36
  • 4
    JavaScript is not PHP. You do not need a `$` prefix on your variable names, but you do need to use `var` to declare them as local variables. Especially the `$Result`. – Bergi Sep 20 '16 at 11:37
  • You aren't *doing* anything with the return value, which won't help. – Dave Newton Sep 20 '16 at 11:38
  • You should not need that condition inside the loop - just make the recursive call. Your function figures out itself whether it's called on an object/array or not. Avoid code duplication. – Bergi Sep 20 '16 at 11:40
  • Your `replace` rules are weird. What are they good for? – Bergi Sep 20 '16 at 11:41
  • I am using JSON, after they are URI encoded. The problem that I found with JSON is that it doesn't handle storing thing like link breaks well. The $ prefix is a habit from PHP but has not cause me any error or issues to date. Which condition is not needed? This could be an array of object that contain other objects. Which result am I not doing anything to? – Luke Pring Sep 20 '16 at 11:45
  • I think I needed replace there because I was using encodeURI instead of encodeURIComponent – Luke Pring Sep 20 '16 at 11:46
  • Bergi, if you would like to make your comment an answer I will mark it as correct. The problem was indeed that script overriding the $Result variable as was fixed by prefixing var to the variable. – Luke Pring Sep 20 '16 at 14:25

0 Answers0