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
Solved

JavaScript: Add item to BEGINNING of object

Posted on 2014-01-20
3
382 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
  • 2
3 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 400 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 100 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Button and js nou working 3 20
Html CheckBox obtain Its Value 5 28
html Uncheck Checkbox 2 25
How to filter by key press ? 6 24
This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
This article discusses how to create an extensible mechanism for linked drop downs.
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…

829 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