I have an js, object which is something like this:
function test{
this.variable = {};
this.populate = function(){
// do some crap....
// and i populate the object like this
this.variable{xyz..} = new object();
}
this.outputThecrap(){
for (var key in data) {
if (data.hasOwnProperty(key)) {
if(data[key].idParent != '0'){
//do some stuff
}
}
}
}
this.addSomeOnBeginigQQ(){
// how do i do that!!!!Q_Q
this.variable{blabla...} = new blabla();
}
}
now after I populate the object like
var t = new test();
t.populate();
t.addSomeOnBegining();
t.outputThecrap();
I get the problem that the added properties wind up on the end of the loop … and I need them to be on the top
Anyone has some idea how to solve this?
UPDATE:
The structure of the object is not open to change. i cant use the array as a container either, that s out of question.
If you want a stack, you will need to use an
Array– a list with a defined order. Object properties have none in JavaScript, there is nothing like “associative arrays”. Also, you should the prototype.You can set properties of array just as you do with objects, but the property names need to be numerically (i.e. integers). You then loop over them with a
for-loop.Arrayobjects also have some extra methods, for example to add items in the beginning or the end (which I have used below):Then use it like this:
The “ordered key array” looks like this: