JavaScript: Add item to BEGINNING of object (NOT ARRAY)

How can I add an item to the beginning of an object?  This adds it to the end:
var obj = {"O":"-","R":"-","L":"-","D":"-"};

Open in new window

I was advised to use unshift() however that seems to only work with arrays.  This must stay an object, and the desired order must be maintained.
LVL 16
Who is Participating?
leakim971Connect With a Mentor PluritechnicianCommented:
Test page :

var obj = {"O":"-","R":"-","L":"-","D":"-"};// YOUR LINE

var bck = obj;
obj = {};

obj.W="-";// YOUR LINE

for(var i in bck) obj[i] = bck[i];

alert(JSON.stringify(obj));// YOUR LINE

Open in new window

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.