【问题标题】:How to shift an object on top position in existing array in javascript, if the object have some specific key如果对象具有某些特定键,如何在javascript中现有数组的顶部位置移动对象
【发布时间】:2021-09-11 06:23:41
【问题描述】:

我有两个数组friend[] 和messages[],并且我已经根据他们的朋友ID 在朋友数组中插入了消息数组值。现在,主要数组是朋友数组,其中包括一些消息,比如有些朋友有消息,有些没有。但是我想根据消息 created_Date 对朋友数组进行排序,而那些在他们的对象中没有任何消息的朋友保持不变。 这是我的代码:

data.sort((a, b) => (a.lastMessage ? new Date(a.lastMessage.createdAt).getTime() < new Date(b.lastMessage.createdAt).getTime() : '') ? 1 : -1)

这是我的输出,未根据最后一条消息日期排序:

{
"data": [
    {
        "block": false,
        "user": {
            "_id": "60ed4a8c1e0812bb34674983",
            "profilePic": "",
            "firstName": "sachin",
            "lastName": ""
        },
        "friend": {
            "_id": "612d11535c77de82c8a68b0d",
            "profilePic": "fsjalk;gja./.coj",
            "firstName": "Sachin4",
            "lastName": "Kumar"
        }
    },
    {
        "block": true,
        "user": {
            "_id": "60ed4a8c1e0812bb34674983",
            "profilePic": "",
            "firstName": "sachin",
            "lastName": ""
        },
        "friend": {
            "_id": "60ed4838ea237d0b40a16491",
            "profilePic": "",
            "firstName": "sachin",
            "lastName": ""
        }
    },
    {
        "block": false,
        "user": {
            "_id": "60ed4a8c1e0812bb34674983",
            "profilePic": "",
            "firstName": "sachin",
            "lastName": ""
        },
        "friend": {
            "_id": "612d108d14521e598838690e",
            "profilePic": "",
            "firstName": "Sachin3",
            "lastName": "Kumar"
        },
        "lastMessage": {
            "message": "This is my sixth chat.",
            "createdAt": "2021-09-06T13:47:50.082Z"
        }
    }
]

}

【问题讨论】:

    标签: javascript node.js ecmascript-6


    【解决方案1】:

    使用Array.filter 将不带lastMessage 的元素连接到带有lastMessage 的已排序元素。

    这是minimal reproducable example

    const data = getData();
    const sorted = data.filter(d => d.lastMessage)
      .sort((a, b) =>
        new Date(a.lastMessage.createdAt) -
        new Date(b.lastMessage.createdAt))
      .concat(data.filter(d => !d.lastMessage));
    
    document.querySelector(`pre`).textContent =
      JSON.stringify(sorted, null, 2);
    
    function getData() {
      return [
        {
          "user": {
            "_id": "60ed4a8c1e0812bb34674983",
          }
        },
        {
          "user": {
            "_id": "60ed4a8c1e0812bb34674983",
          },
          "lastMessage": {
            "createdAt": "2021-09-06T13:47:50.082Z"
          }
        },
        {
          "user": {
            "_id": "60ed4a8c1e0812bb34674983",
          },
        },
        {
          "user": {
            "_id": "60ed4a9c140e19bb34674983",
          },
          "lastMessage": {
            "createdAt": "2020-04-02T23:11:12.154Z"
          }
        },
      ];
    }
    &lt;pre&gt;&lt;/pre&gt;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多