I'm using a script that sorts an JSON in alphabetical order with an indicative header, as shown in question however I am not able to make it work in harmony with prototype, I get the error:
Cannot read property '0' of undefined
I have no idea how to solve it because in fact the script is not mine, so I'm lost, I wanted to solve it. Here's the script:
var itens = JSON.parse('[{"user_id":"3333","username":"cacau"},{"user_id":"3333","username":"balmer"}]');
itens.sort(function(a,b){
return a.username.localeCompare(b.username);
});
var letras = {};
for(var i in itens) {
console.log(itens);
var l = itens[i].username[0].toUpperCase();
letras[l] = letras[l] || [];
letras[l].push(itens[i]);
}
for(var letra in letras) {
document.write('LETRA ' + letra + "<br />");
for(var k in letras[letra]) {
document.write(letras[letra][k].username + "<br />");
}
document.write('<br />');
}
A fiddle reproducing the error link
Note: lib prototype.js is being used along with this code.