The exemploLista
that you are using and is created with the notation {...}
is a object and not a array. In javascript objects are basically hash maps ({key: value}) and according to the specification that defines the syntax/rules of the language an object is "an unordered collection of properties with values".
So the way you built your object exemploLista
there is no way to leave it in any desired order, you must consider that the values will always come out in random order, the fact that you have tested and the values have been printed in the order in which you put them in the object (Object {Igor: "exemplo1.jpg", Joao: "exemplo2.jpg", Lucas: "exemplo3.jpg"}
) can be considered pure chance and can (read will) vary according to the browser even according to the version of the same.
If you want to ensure the order a good option is to use arrays true, an outline of how you could do whatever you want:
var exemploLista = [];
exemploLista[1] = {nome: "Igor", imagem: "exemplo1.jpg"};
exemploLista[2] = {nome: "Joao", imagem: "exemplo2.jpg"};
exemploLista[0] = {nome: "Lucas", imagem: "exemplo3.jpg"};
In the above code Lucas would be added last, but would be in the first position of the array. To remove "Lucas" from the array if it already existed before it was added you need to iterate over the sample arrayList, check at each position if there is an object with the name Lucas, remove if it exists and after the end of the loop add Ucas at the desired position. Note that you would also have to rearrange the positions of the array so that it does not become empty elements. I will not add more code and explanations because the answer is already quite long so I suggest you read the links about objects and arrays that I passed so that you understand the difference and then post a new more specific question if you still need.
Although it’s a great solution I don’t think it’s a good idea for you to use the term
Class
to refer to the construction functionLista
, after all javascript has no classes and this will probably confuse the user who asked the question (see that he was already confusing objects with arrays) and other people.– BrunoRB
It worked, but how could I treat this data? I’m trying so:
listaExemplo = new Lista({"Igor": "exemplo1.jpg", "Joao": "exemplo2.jpg"});
but while using$.each(listaExemplo, function(index, value) {
 $("body").append(index+" - "+value);
 });
the following is returned: Igor - example1.jpgJoao - example2.jpgset - Function (a,t){var e={};for(var o in this)this.hasOwnProperty(o)&&o!= a&&(e[o]=this[o],delete this[o]);this[a]=t;for(var o in e)this[o]=e[o]}– Igor
@Brunorb, I understand that it is not correct to call this type of class structure, but in ECMA5, this type of structure is the closest to a class, so much so that in this Guide: Introduction to Object-Oriented Javascript, the author calls it
Classe
, perhaps because it believes that for didactic purposes it is valid to do this corelation between OO and Prototype Orientation.– Tobias Mesquita
@Igor, as pointed out by Bruno, you cannot guarantee the order of the properties of an object, on the other hand, the object
Map
secured this order, but it is only supported by the latest versions of Browsers, what you can do is use the above implementation to implement your ownHashMap
with the necessary characteristics and methods.– Tobias Mesquita
@Tobymosque If I implement the example you posted, it will work in all browser versions or only the latest ones?
– Igor
@Tobymosque another question. If I wanted, instead of leaving the object at the beginning, leave it at the end, what would be the adaptation in the code? Thank you!
– Igor