Javascript: how to unshift an object?


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

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

Example:

var a={a:1,b:2}
var b = object_unshift(a,'c',3)

Result:

Object {c: 3, a: 1, b: 2}

No comments yet.

Leave a Reply