Solved

JavaScript: Add item to BEGINNING of object

Posted on 2014-01-20
3
386 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 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…

726 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