1

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?"]}
10
  • You show the variable messages as a single object that contains a single message for one particular person. Do you have an array of these somewhere that cover all the messages from all the people? Commented Feb 28, 2014 at 20:29
  • I could create one very easily. Just by iterating over the object property message like: messages['message']; Commented Mar 1, 2014 at 0:15
  • What confuses things is your definition of messages is a SINGLE message. You show no data structure that has multiple messages in it so we're not sure what data you want us to search for just the "John Doe" messages. The one answer you have so far made up such a data structure. I figured you should show us what you data structure is before I offer code. Commented Mar 1, 2014 at 0:23
  • Ah you're right. I can imagine that's pretty confusing. Thanks for the feedback. I'll edit my question. Commented Mar 1, 2014 at 0:29
  • 1
    Well, you need to be clear about both input data format, function to be performed on it and desired output data format for the returned value. Right, now I see no clarify on either data format. Commented Mar 1, 2014 at 1:23

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, you've provided about the same answer as above but a cleaner one. Still, this returns a big list of objects and that's not what i'm looking for. I want to call this method and store it in different variables in the code. But I never know how much people there are in a chat. The only way that I don't have to know how much people are in the game is it iterate over a keyvalue pair. Something like: {"Wence":["message", "and another"],"Alice":["more, "messages]}
@user3263723 - I give up. I asked for the ACTUAL data structure you are using so I could write code to that. I thought you provided that so I took the time to write an answer, but apparently you didn't so now you don't like my code. Apparently, you don't get it, but it is a waste of everyone's time when you don't supply us with the actual data you're trying to search. How can we possibly answer the question when you won't give us the most rudimentary information about what your data looks like?
@user3263723 - OK, now that you've simplified the question a lot and provided input and output data formats, here's a simple function to do that data reorganization.
0

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"}]

8 Comments

I'm sorry, but this only seems to return an empty array
I posted a working example, you'd need to show exactly what your code looks like for me to help further.
Ah, I see your thinking. It's just only that I don't know how many users there are and what there name is. So I can't set up an var in advance for them. With one variable I've to be able to retrieve all messages grouped by persons. More like an output like this: {"wence":["A","B","C"],"John":["D","E"]}
That's fine, I just made an example that you could copy paste into JS console right now and execute. However, you could replace that hardcoded 'Wence' with your deelnemers array. Or are you saying your equivalent allMessages array is different. Or even rather that the last object you wrote is what you want the output object to be?
No not at all. I think that I see what I did wrong now. I've passed an array as an argument rather than a string. Will try again right now. Thanks for the involvement anyways!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.