JavaScript Meteor: How to return only one entry for each value from collection

I am currently working on a Meteor application that also has a chat functionality.

I want to have a list of all conversations that show the most recent message in each conversation. (Similar to Sample of a basic conversation overview)

Let's say I have a collection Messages with a variable conversationId. New messages get assigned a new conversationId and any replies will get assigned the conversationId of the first message.

To achieve this conversation overview, my question is, how do I return from my collection only the most recent entry for each conversationId?

This is where I am stuck:

Template.tabsTwo.helpers({
  messages: function () {

    //to retrieve all messages from the logged in user, then retrieve the conversationIDs and drop duplicates
    var userMessages = Messages.find({senderId: Meteor.userId()}, {sort: {date_created: -1, subject: -1}});

    var userConversationIds = userMessages.map((function(a) {
      return a.conversationId;
    }));

    var uniqueConversationIDs = [];

    $.each(userConversationIds, function(i, el){
        if($.inArray(el, uniqueConversationIDs) === -1) uniqueConversationIDs.push(el);
    });

    return Messages.find({conversationId: {$in:uniqueConversationIDs}}, {sort: {date_created: -1}});
  }
});

This still gives me back all messages. I am asking myself right now if I can modify this query to make it work or if I need to approach this differently (e.g. do a loop and a .findOne query)?

(I have tried many things and searched for answers in the docs and SO but have troubles getting this right. Any help would be greatly appreciated.)

Answer:1

Does anyone knows how to directly call a array key of a Map Object. As shown in below code, I can map.get(arr), but not map.get([0, 1, 2, 3]) const map = new Map() const arr = [0,1,2,3] map....

Does anyone knows how to directly call a array key of a Map Object. As shown in below code, I can map.get(arr), but not map.get([0, 1, 2, 3]) const map = new Map() const arr = [0,1,2,3] map....

  1. call array object javascript
  2. call object array php
  3. call array inside object
  4. call an array object
  5. java call array object
  6. call_user_func_array object
  7. call_user_func_array object method
  8. jni call object array method
  9. call_user_func_array object oriented
  10. call_user_func_array object php
  11. array object call function
  12. python call array object

In my console's browser, when I do : console.log(typeof(typeof)) it returns an error Uncaught SyntaxError: Unexpected token ')' Why is that? Why can't I get typeof's type ?

In my console's browser, when I do : console.log(typeof(typeof)) it returns an error Uncaught SyntaxError: Unexpected token ')' Why is that? Why can't I get typeof's type ?

ul element that has dynamically loaded li a children, sometimes the li a populate empty innerHTML. How do I remove all of the li elements that have an empty a child? Current (errors Uncaught ...

ul element that has dynamically loaded li a children, sometimes the li a populate empty innerHTML. How do I remove all of the li elements that have an empty a child? Current (errors Uncaught ...

  1. javascript remove empty string from array
  2. javascript remove empty lines
  3. javascript remove empty space
  4. javascript remove empty values from object
  5. javascript remove empty objects from array
  6. javascript remove empty space from string
  7. javascript remove empty characters from string
  8. javascript remove empty objects
  9. javascript remove empty tags
  10. javascript remove empty p tags
  11. javascript remove empty slots from array
  12. javascript remove empty in array
  13. javascript remove empty html tags
  14. javascript remove empty paragraphs
  15. javascript remove empty value from array
  16. javascript remove empty rows from table
  17. javascript remove empty item from array
  18. javascript remove empty text nodes
  19. javascript remove empty lines from textarea

I'm building a Node.js app (Node v10.11.0) and want to do it in a OOP-way without using Typescript. So I've built some classes like this: class Component { constructor(param1, param2, param3) { ...

I'm building a Node.js app (Node v10.11.0) and want to do it in a OOP-way without using Typescript. So I've built some classes like this: class Component { constructor(param1, param2, param3) { ...

  1. dynamically call javascript class
  2. dynamically add javascript class
  3. javascript dynamically create class
  4. javascript dynamically add class to element
  5. dynamically extend class javascript
  6. dynamically instantiate class javascript
  7. javascript dynamically set class name
  8. javascript dynamically change class
  9. javascript dynamically remove class
  10. javascript dynamically generate class
  11. javascript dynamically add class to div
  12. javascript dynamically set class
  13. javascript dynamically create class object
  14. dynamically load class javascript
  15. javascript dynamically create css class
  16. dynamically set css class javascript
  17. dynamically apply css class javascript
  18. dynamically update css class javascript
  19. javascript dynamically add style class
  20. javascript dynamically change css class