简体   繁体   English

查找数组中的值满足条件的数组索引

[英]Find index of array where value in array meets condition

The client is receiving a message from socket.io on the server.客户端正在从服务器上的 socket.io 接收消息。 The data of this message has this object:这个消息的数据有这个对象:

{
    from,
    text,
    dateTimeSent,
    chatId
}

These refer to data about a chat message that is being sent from another user on the system.这些是指有关从系统上的另一个用户发送的聊天消息的数据。

On the client-side the user is storing information about all of the chats they are participants of.在客户端,用户存储有关他们参与的所有聊天的信息。 This data comes from a MongoDB that has a collection called chats , in which, each chat has an _id.这些数据来自一个 MongoDB,它有一个名为chats的集合,其中每个聊天都有一个 _id。

So basically, the client-side is storing information all information about each chat they have access to, including the _id of the chat.所以基本上,客户端正在存储有关他们有权访问的每个聊天的所有信息的信息,包括聊天的 _id。

How would i use chatId received on the socket, then search for that ID in the array that stores the _id of the chat that this message should go into?我将如何使用在套接字上收到的chatId ,然后在存储此消息应进入的聊天的 _id 的数组中搜索该 ID?

Is there a JavaScript function where you can search for the value of a specific field in an array?是否有 JavaScript 函数可以在数组中搜索特定字段的值?

The following is the array that the user is storing on their client.以下是用户存储在其客户端上的数组。 How would i search for the value of chatId in this array?我将如何在这个数组中搜索 chatId 的值?

[

    [
        "0",
        {
            "_id":"5ccb37c82eab402834818e8f",
            "participants":[
                {
                    "_id":"5ccb37c82eab402834818e91",
                    "userEmail":"test2@gmail.com"
                },
                {
                    "_id":"5ccb37c82eab402834818e90",
                    "userEmail":"test@gmail.com"
                }
            ],
            "chatType":"chat",
            "messages":[
                {
                    "dateTimeSent":"2019-05-02T19:50:19.000Z",
                    "_id":"5ccb49fba2129f160cb22ac8",
                    "text":"",
                    "from":"test2@gmail.com"
                },
                {
                    "dateTimeSent":"2019-05-02T19:50:58.896Z",
                    "_id":"5ccb4a27a2129f160cb22ade",
                    "text":"dfdf",
                    "from":"test2@gmail.com"
                },
                {
                    "dateTimeSent":"2019-05-02T19:51:03.093Z",
                    "_id":"5ccb4a27a2129f160cb22add",
                    "text":"",
                    "from":"test2@gmail.com"
                },
                {
                    "dateTimeSent":"2019-05-02T19:54:13.070Z",
                    "_id":"5ccb4aeaa2129f160cb22b06",
                    "text":"",
                    "from":"test2@gmail.com"
                },
                {
                    "dateTimeSent":"2019-05-02T21:45:29.498Z",
                    "_id":"5ccb64fbf769fd273c500889",
                    "text":"hi",
                    "from":"test@gmail.com"
                },
                {
                    "dateTimeSent":"2019-05-02T21:45:31.020Z",
                    "_id":"5ccb64fbf769fd273c500888",
                    "text":"hi",
                    "from":"test@gmail.com"
                }
            ],
            "__v":0
        }
    ],
    [
        "1",
        {
            "_id":"5ccb5197a2129f160cb22b13",
            "participants":[
                {
                    "_id":"5ccb5197a2129f160cb22b15",
                    "userEmail":"test2@gmail.com"
                },
                {
                    "_id":"5ccb5197a2129f160cb22b14",
                    "userEmail":"adam2.cole@northumbria.ac.uk"
                }
            ],
            "chatType":"chat",
            "messages":[
                {
                    "dateTimeSent":"2019-05-02T20:22:50.633Z",
                    "_id":"5ccb519ca2129f160cb22b1b",
                    "text":"fgfhgfbvb",
                    "from":"test2@gmail.com"
                },
                {
                    "dateTimeSent":"2019-05-02T20:22:51.924Z",
                    "_id":"5ccb519ca2129f160cb22b1a",
                    "text":"fgfhgfbvb",
                    "from":"test2@gmail.com"
                },
                {
                    "dateTimeSent":"2019-05-02T20:22:52.751Z",
                    "_id":"5ccb519ca2129f160cb22b19",
                    "text":"fgfhgfbvb",
                    "from":"test2@gmail.com"
                }
            ],
            "__v":0
        }
    ]

]

Find index of array where value in array meets condition查找数组中的值满足条件的数组索引

You're probably looking for array.findIndex() .您可能正在寻找array.findIndex() Give it an array, and a function that runs for each item.给它一个数组和一个为每个项目运行的函数。 Return true when it comes across the ID you're looking for.遇到您要查找的 ID 时返回true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM