javascript - swap dictionary changes type to integer

Viewed 27

So I am using this code for inverting a dictionary - which I found on SO.

function g_swap_dictionary ( dict ) {
    let ret = {};
    for(var key in dict){
        ret[dict[key]] = key;
    }
    return ret;
}

But if I have this dictionary :

 [object Object] {
    0: 0,
    1: 2,
    2: 4,
    3: 1,
    4: 3
}

and swap it I get this :

[object Object] {
    0: "0",
    2: "1",
    4: "2",
    1: "3",
    3: "4"
}

so values change to string type. Since I want 'g_swap_dictionary' to be as generic as possible - how can I fix that?

1 Answers
Related