If I attach an additional property to an object, does it have to go at the end, or can I put it at a specified position.
function reassign(obj){
delete obj.c;
obj.c = "new three";
var t = "<table border=\"1\">";
for (var i in obj){
t += "<tr><td>" + obj[i] + "</td></tr>";
}
t += "</table>";
return t;
}
var obj = {
a : "one",
b : "two",
c : "three",
d : "four"
};
var reassigned = reassign(obj);
document.write(reassigned);
Would it be possible somehow to put the new obj.c in the third position again after it’s been deleted, rather than having it appended to the end? Of course I could skip the delete part. But even without that, I could also for example intend inject alphabeta: "one point five" into the second position. Is it possible? Thanks!
Array elements are ordered; object properties are not. You should never assume that object properties will be iterated in a particular order.
If order is important to you, create a new
order:[]property which contains an array of property names, and you can iterate through that: