Javascript: how to reverse an object?


How to reverse an object in Javascript ?
Here my method:

function object_reverse(o){
    var b=[],c=[],d={}
 
    for(var i in o){
        b.unshift(i);
        c.unshift(a[i]);
    }
     
    for(var i in b){
        d[b[i]]=c[i]
    }
    return d;
}

Example:

var a={a:1,b:2}
var b=object_reverse(a)

Result:

Object {b: 2, a: 1}

Leave a Reply