Question

I've got an Array object that looks like this(amount of objects is about 100x bigger)

var messages =[
{
        date: "12-12-12",
        time: "12:34:10",
        name: "Wence",
        message: "Howdy matey"
    },
{
        date: "12-12-12",
        time: "12:34:10",
        name: "Alice",
        message: "Everything allright?"
    }
{
        date: "12-12-12",
        time: "12:34:10",
        name: "Wence",
        message: "I'm fine and you?"
    }];

With this object I want to create a new keypair structure like this:

{"wence":["Howdy matey","I'm fine and you?"],"Alice":["Everything allright?"]}
Was it helpful?

Solution

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).

OTHER TIPS

Assuming an array call `allMessages' that contains the set of all your messages objects to search:

var messagesSentByNames = allMessages.reduce(function(relevantMsgs,message) {

  for(var i = 0; i < deelnemers.length; i++) {
    if(message.name == deelnemers[i]) {
      relevantMsgs.push(message);
      break;
    }
  }; 

  return relevantMsgs;
}, []);

If you want something reusable:

/**
 * names: array of Strings or String representing name(s) to search
 * messagesToSearch: array of message objects to find matches in
 */
function getMessagesSentBy(names, messagesToSearch) {
  //Can pass in a single name as string
  if(typeof names === 'string') {
    names = [names];
  }

  return messagesToSearch.reduce(function(relevantMsgs,message) {

    for(var i = 0; i < names.length; i++) {
      if(message.name === names[i]) {
        relevantMsgs.push(message);
        break;
      }
    }; 

    return relevantMsgs;
  }, []);
}

Example

    var allMessages = [
      {date: "12-12-12",time: "12:34:10",name: "Wence", message: "A"},
      {date: "12-12-12",time: "12:34:10",name: "Bob", message: "B"},
      {date: "12-12-12",time: "12:34:10",name: "Wence", message: "C"},
      {date: "12-12-12",time: "12:34:10",name: "Sanderson", message: "D"}];

    var users = ['Wence','Bob'];

    var wenceMessages = getMessagesSentBy(users, allMessages);

    console.log(wenceMessages);

Output:

[{date: "12-12-12",time: "12:34:10",name: "Wence", message: "A"},
 {date: "12-12-12",time: "12:34:10",name: "Bob", message: "B"},
 {date: "12-12-12",time: "12:34:10",name: "Wence", message: "C"}]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top