数组和排序

时间:2018-06-09 22:35:29

标签: javascript jquery

我想要实现的是一个消息传递应用程序,在您选择所需的消息对话之前,您会看到所有最近消息的列表,并预览最新消息接收/发送。

问题是我的脚本遍历数组并发布所有结果,我想要做的是它将我的电话号码(82784)与联系人分组,每个联系人只显示1个结果,并且对于不在我的联系人列表中的联系人,他们的name应该是他们的电话号码。

我添加了两个阵列,我的联系人和发送的消息。以及它的外观和外观的两个图像。

How it looks at the moment

How I would want it to look like

通讯录数组

contacts = {
  [1] = {contact_name = "Rudolf Albinsson", contact_number = "32682"},
  [2] = {contact_name = "Svea Engström", contact_number = "94784"},
  [3] = {contact_name = "Theodor Sylvia", contact_number = "45270"},
  [4] = {contact_name = "Kai Catrine", contact_number = "12527"}
},

消息数组

messages = {
  [1] = {message_sender = 32682, message_sender_name = "Rudolf Albinsson", message_receiver = 82784, message_receiver_name = "Ivanov Valkov", message_message = "Test 1", message_read = "0", message_datestamp = "2018-06-09 05:45:49"},
  [2] = {message_sender = 32682, message_sender_name = "Rudolf Albinsson", message_receiver = 82784, message_receiver_name = "Ivanov Valkov", message_message = "Test 2", message_read = "0", message_datestamp = "2018-06-09 05:46:12"},
  [3] = {message_sender = 82784, message_sender_name = "Ivanov Valkov", message_receiver = 32682, message_receiver_name = "Rudolf Albinsson", message_message = "Test 3", message_read = "0", message_datestamp = "2018-06-09 05:47:46"},
  [4] = {message_sender = 82784, message_sender_name = "Ivanov Valkov", message_receiver = 32682, message_receiver_name = "Rudolf Albinsson", message_message = "Test 4", message_read = "0", message_datestamp = "2018-06-09 05:48:01"},
  [5] = {message_sender = 32682, message_sender_name = "Rudolf Albinsson", message_receiver = 82784, message_receiver_name = "Ivanov Valkov", message_message = "Test 5", message_read = "0", message_datestamp = "2018-06-09 05:48:36"},
  [6] = {message_sender = 62649, message_sender_name = "Hamid Hamdi", message_receiver = 82784, message_receiver_name = "Ivanov Valkov", message_message = "Test 6", message_read = "0", message_datestamp = "2018-06-09 05:48:55"},
},

代码

for(let i = 0; i < PlayersData.messages.length; i++){
  if(PlayersData.messages[i].message_receiver == PlayersData.phone && PlayersData.messages[i].message_sender == contact_number){
     Contacts.find("ul.messages").append('<li class="contact">'+ PlayersData.messages[i].message_message +'</li>');
  }else if(PlayersData.messages[i].message_receiver == contact_number && PlayersData.messages[i].message_sender == PlayersData.phone){
     Contacts.find("ul.messages").append('<li class="me">'+ PlayersData.messages[i].message_message +'</li>');
  }
}

1 个答案:

答案 0 :(得分:0)

这是一个可以帮助您的代码。 您必须通过替换console.log行来更改要显示数据的方式。

const handledSendersNumbers = []
// We run over all messages
for (let messageIndex = messages.length - 1; messageIndex >= 0; messageIndex--) {
    const message = messages[messageIndex]

    // We check that the number of the sender has not been handled yet
    // If it's not the case we have to display his message
    if (handledSendersNumbers.indexOf(message.message_sender.toString()) === -1) {
        // We look for a contact corresponding to the sender number
        let foundContact
        for (const contact of contacts) {
            if (contact.contact_number === message.message_sender.toString()) {
                foundContact = contact
                break
            }
        }

        if (foundContact) { // if a contact has been found, we display his name and the message
            console.log(foundContact.contact_name, message.message_message)
        } else { // if no contact has been found, we display his number and the message
            console.log(message.message_sender, message.message_message)
        }

        // We add the number of the sender to the liste of handled numbers
        handledSendersNumbers.push(message.message_sender.toString())
    }
}