0
I created this example to illustrate an attempted listing of JSON
seeking to separate key and value:
var meuArray = {
"1": "Administra\u00e7\u00e3o",
"2": "Agronomia",
"3": "Arquitetura e Urbanismo",
"4": "Artes C\u00eanicas",
"5": "Artes Visuais",
"6": "Biblioteconomia",
"7": "Biologia",
"8": "Biomedicina",
"9": "Biotecnologia",
"10": "Ci\u00eancia da Computa\u00e7\u00e3o",
"11": "Ci\u00eancias Ambientais",
"12": "Ci\u00eancias Biol\u00f3gicas",
"13": "Ci\u00eancias Cont\u00e1beis",
"14": "Ci\u00eancias Econ\u00f4micas",
"15": "Ci\u00eancias Sociais",
"16": "Comunica\u00e7\u00e3o Social",
"17": "Dan\u00e7a",
"18": "Design de Ambientes",
"19": "Design de Moda",
"20": "Design Gr\u00e1fico",
"21": "Dire\u00e7\u00e3o de Arte",
"22": "Direito",
"23": "Ecologia e An\u00e1lise Ambiental",
"24": "Educa\u00e7\u00e3o F\u00edsica",
"25": "Educa\u00e7\u00e3o Musical",
"26": "Enfermagem",
"27": "Engenharia Ambiental",
"28": "Engenharia Civil",
"29": "Engenharia de Alimentos",
"30": "Engenharia de Computa\u00e7\u00e3o",
"31": "Engenharia de Minas",
"32": "Engenharia de Produ\u00e7\u00e3o",
"33": "Engenharia de Software",
"34": "Engenharia El\u00e9trica",
"35": "Engenharia Florestal",
"36": "Engenharia Mec\u00e2nica",
"37": "Engenharia Qu\u00edmica",
"38": "Estat\u00edstica",
"39": "Farm\u00e1cia",
"40": "Filosofia",
"41": "F\u00edsica",
"42": "Fisioterapia",
"43": "Geografia",
"44": "Gest\u00e3o da Informa\u00e7\u00e3o",
"45": "Hist\u00f3ria",
"46": "Jormalismo",
"47": "Letras",
"48": "Matem\u00e1tica",
"49": "Matem\u00e1tica Industrial",
"50": "Medicina",
"51": "Medicina Veterinaria",
"52": "Museologia",
"53": "M\u00fasica",
"54": "Musicoterapia",
"72": "newCi\u00eancia",
"55": "Nutri\u00e7\u00e3o",
"56": "Odontologia",
"57": "Pedagogia",
"58": "Psicologia",
"59": "Publicidade e Propaganda",
"60": "Qu\u00edmica",
"61": "Rela\u00e7\u00f5es P\u00fablicas",
"62": "Servi\u00e7o Social",
"63": "Sistemas de Informa\u00e7\u00e3o",
"64": "Zootecnia"
};
function print(meuArray) {
var tamPerson = Object.keys(meuArray).length;
// alert(tamPerson);
for (var i = 0; i < tamPerson; i++) {
$("#parag").append((Object.keys(meuArray)[i]) + " " + meuArray[i] + "<br />");
};
};
print(meuArray);
// $("#parag").append((Object.keys(meuArray)[0]) + " " + meuArray[0] + "<br />");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<p id="parag"></p>
I forgot to say an important detail, that list is in alphabetical order (in value) so I need the keys and respective values to appear in that same order of the original 'associative array'.
You may notice that in the Gabriel and Samir examples the 72 newCcience was at the end of the list (outside the original order) when in fact it is in the 'associative array' between the keys 54 and 55, and it would be there that it should be listed because this list goes to a select and needs to be in alphabetical order.
To print the list in the same order as the associative array with its respective keys and values, Somebody save me?
Example also available on jsfiddle
As for the ordination, in this OS question in English People say that the javascript specification does not guarantee the ordering of the properties of the object in the same way it was inserted, that is... an object contains an unordered set of properties. So I see the following solutions: - renumber the keys in order to sort this element 72 in its correct order; or - change the object structure, as @user31050 suggested below, in order to create its arbitrary element order.
– Paulo Elias
That @Brunorb answer to a similar question explains well the question of ordering the properties of the object.
– Paulo Elias