Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

JavaScript: Add item to BEGINNING of object

Posted on 2014-01-20
3
Medium Priority
?
397 Views
Last Modified: 2014-01-20
How can I add an item to the beginning of an object?  This adds it to the end:
var obj={"b":2,"c":3,"d":4};
obj.a=1;
alert(JSON.stringify(obj));

Open in new window

0
Comment
Question by:hankknight
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 1600 total points
ID: 39795839
Test page : http://jsfiddle.net/KpA8v/

var obj = {"b":2,"c":3,"d":4};
var bck = obj;
obj = {};
obj.a=1;
for(var i in bck) obj[i] = bck[i];
alert(JSON.stringify(obj));

Open in new window


But if you really need to maintain order use an array : http://jsfiddle.net/KpA8v/1/
var obj = [{"b":2},{"c":3},{"d":4}];
var o = {a:1};
obj.unshift(o);
alert(JSON.stringify(obj));

Open in new window

0
 
LVL 14

Assisted Solution

by:brendanmeyer
brendanmeyer earned 400 total points
ID: 39795840
0
 
LVL 82

Expert Comment

by:leakim971
ID: 39795848
You accepted a bad answer.
unshift is for array not for object.
Additionaly brendanmeyer answer don't follow the rules (bind links) :
http://support.experts-exchange.com/customer/portal/articles/1162518

You should ask a moderator o reopen the question and close it differently or wait for brendanmeyer to give more details about its answer.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question