OK, I'm starting over one more time with your newly described input and output data examples:
var messages = [{
date: "12-12-12",
time: "12:34:10",
name: "Wence",
message: "Hi Bob"
}, {
date: "12-12-12",
time: "12:34:10",
name: "Bob",
message: "Howdy partner"
} {
date: "12-12-12",
time: "12:34:10",
name: "Bob",
message: "Howdy matey."
}];
function structureMessages(list) {
var data = {}, item;
for (var i = 0; i < list.length; i++) {
item = list[i];
// if no entry for this name yet, initialize it to an empty array
if (!data.hasOwnProperty(item.name)) {
data[item.name] = [];
}
// add this message to the array for this name
data[item.name].push(item.message);
}
return data;
}
// convert the data format to be messages organized by name
var messagesByName = structureMessages(messages);
console.log(messagesByName["Wence"]);
FYI, for the data above, this creates a data structure like this:
{Wence: ["Hi Bob"], Bob: ["Howdy partner", "Howdy matey."]}
Each key in the object is a user name. The data for each key is an array of messages by that user. It might be more useful if it was an array of message objects from the original array because then you'd have the date and time info along with it. You can modify the above code to do that by just changing this line:
data[item.name].push(item.message);
to this:
data[item.name].push(item);
If you did it this way, each key in the object is still a user name, but the data for each key is an array of message objects (which has the message in it, but also the other metadata).