簡體   English   中英

重新映射json對象以進行消息傳遞

[英]re-map json object for messaging

我正在嘗試將Twitter直接消息數據重新映射到mongodb的對話中。

"events": [
        {
            "type": "message_create",
            "id": "1023372540847312900",
            "created_timestamp": "1532826001737",
            "message_create": {
                "target": {
                    "recipient_id": "605675237"
                },
                "sender_id": "1020464064684806146",
                "source_app_id": "268278",
                "message_data": {
                    "text": "all right mate thank you too",
                    "entities": {
                        "hashtags": [],
                        "symbols": [],
                        "user_mentions": [],
                        "urls": []
                    }
                }
            }
        },
        {
            "type": "message_create",
            "id": "1023372444210524164",
            "created_timestamp": "1532825978697",
            "message_create": {
                "target": {
                    "recipient_id": "1020464064684806146"
                },
                "sender_id": "605675237",
                "message_data": {
                    "text": "Okay thank you mate, this is distinquish",
                    "entities": {
                        "hashtags": [],
                        "symbols": [],
                        "user_mentions": [],
                        "urls": []
                    }
                }
            }
        },
        {
            "type": "message_create",
            "id": "1023372325150965765",
            "created_timestamp": "1532825950311",
            "message_create": {
                "target": {
                    "recipient_id": "1020464064684806146"
                },
                "sender_id": "69565292",
                "message_data": {
                    "text": "Hello strow bree how are you",
                    "entities": {
                        "hashtags": [],
                        "symbols": [],
                        "user_mentions": [],
                        "urls": []
                    }
                }
            }
        },
        {
            "type": "message_create",
            "id": "1023245595790778372",
            "created_timestamp": "1532795735677",
            "message_create": {
                "target": {
                    "recipient_id": "605675237"
                },
                "sender_id": "1020464064684806146",
                "source_app_id": "268278",
                "message_data": {
                    "text": "Once at a social gathering, Gladstone said to Disraeli",
                    "entities": {
                        "hashtags": [],
                        "symbols": [],
                        "user_mentions": [],
                        "urls": []
                    }
                }
            }
        },
        {
            "type": "message_create",
            "id": "1023245133637140484",
            "created_timestamp": "1532795625491",
            "message_create": {
                "target": {
                    "recipient_id": "1020464064684806146"
                },
                "sender_id": "605675237",
                "message_data": {
                    "text": "On Krat's main screen appeared the holo image of a man, and several dolphins. From the man's shape, Krat could tell it was a female, probably their leader.    \"...stupid creatures unworthy of the name `sophonts.'  Foolish, pre-sentient upspring of errant masters.  We slip away from all your armed might, laughing at your clumsiness!  We slip away as we always will, you pathetic creatures. And now that we have a real head start",
                    "entities": {
                        "hashtags": [],
                        "symbols": [],
                        "user_mentions": [],
                        "urls": []
                    }
                }
            }
        }
    ],
    "apps": {
        "268278": {
            "id": "268278",
            "name": "Twitter Web Client",
            "url": "http://twitter.com"
        }
    }
}

我正在嘗試將此數據更改為此;

[{
    members: [ '605675237', '1020464064684806146' ],
    messages: [
        { author: '605675237', body: 'On Krats main screen appeared the holo image of a man, and several dolphins. From the mans shape, Krat could tell it was a female, probably their leader ...stupid creatures unworthy of the name sophonts  Foolish, pre-sentient upspring of errant masters.  We slip away from all your armed might, laughing at your clumsiness!  We slip away as we always will, you pathetic creatures. And now that we have a real head start' },
        { author: '1020464064684806146', body: 'Once at a social gathering, Gladstone said to Disraeli' }
        { author: '605675237', body: 'Okay thank you mate, this is distinquish' }
        { author: '1020464064684806146', body: 'all right mate thank you too' },
    ]
},
{
    members: ['69565292', '1020464064684806146'],
    messages: [
        { author: '69565292', body: 'Hello strow bree how are you' }
    ]
}]

user_id1必須是sender_id,user_id2必須是收件人_id,但實際上,我可能必須按sender_id和收件人_id對twitter DM對象進行分組。

我怎樣才能輕松解決這個問題的任何建議?

也許我們可以使用Lodash或Underscore輕松解決此重新映射。

解決方案是按用戶ID對消息進行分組,我從ID創建了一個散列以使其適合對象:

const normalized = {};
data.events.forEach(event => {
  // Just of easier access
  const message = event.message_create;

  // Create a temp hash which identify 2 user conversation.
  // The hash is just the 2 ids with a `|` separated them
  const hashedUsers = [message.sender_id, message.target.recipient_id].sort().join('|');

  // The end object you wanted
  const normalizedMessage = {
    author: message.sender_id,
    body: message.message_data.text
  };

  // Check if we already have this 2 users conversations, create a list of their messages
  if (!normalized.hasOwnProperty(hashedUsers)) {
    normalized[hashedUsers] = [normalizedMessage];
  } else {
    normalized[hashedUsers].push(normalizedMessage);
  }
});


// Now we have a normalized object, the keys are the hash we created and the values are their messages
// so we can create the object you wanted:
const output = []
Object.entries(normalized).forEach(([key, value]) => {
  output.push({
    members: key.split('|'),
    messages: value
  })
})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM