如何在Map()的JSON对象中组合通用数据?
我有一些像这样的 JSON 数据:
{
"+2555970315763":{
"uid": "u9weKk62GvXu4Yf66HM2TFXDL5S2",
"phoneNumber": "+2555970315763",
"lastTime": "Mon, 14 Feb 2022 09:40:32 GMT"
},
"+2555970605736":{
"uid": "n3qGtqMWqVXGyZMSFz7HqwtfTDB3",
"phoneNumber": "+2555970605736",
"lastTime": "Mon, 14 Feb 2022 05:32:53 GMT"
},
"+2555973055799":{
"uid": "hGHq2TLCkeWsCWOZ0KdSI6mKbvp2",
"phoneNumber": "+2555973055799",
"lastTime": "Mon, 21 Feb 2022 05:25:46 GMT",
"usage": [
{
"firstTime": "18:04:34",
"count": 1,
"date": "2022-02-19"
},
{
"firstTime": "14:53:08",
"count": 6,
"date": "2022-02-21"
},
{
"count": 4,
"date": "2022-02-22",
"firstTime": "12:53:56"
},
{
"date": "2022-02-25",
"count": 4,
"firstTime": "12:12:03"
},
{
"firstTime": "12:35:04",
"date": "2022-02-26",
"count": 34
},
{
"count": 31,
"firstTime": "11:32:03",
"date": "2022-02-27"
}
]
},
"+2555974235764":{
"uid": "uUWJvN5XVwWGfNiC6UuwlmVVTO73",
"phoneNumber": "+2555974235764",
"lastTime": "Wed, 16 Feb 2022 06:04:54 GMT",
"usage": [
{
"count": 2,
"date": "2022-02-16",
"firstTime": "12:15:38"
},
{
"date": "2022-02-17",
"firstTime": "11:45:19",
"count": 43
},
{
"count": 148,
"firstTime": "11:33:53",
"date": "2022-02-19"
},
{
"count": 127,
"firstTime": "11:42:46",
"date": "2022-02-20",
"dow": "Sunday"
},
{
"firstTime": "12:18:28",
"count": 42,
"date": "2022-02-22",
}
]
},
"+2555979295712":{
"uid": "Cx63RRHQYpMMwfAbi0ebqledk5G3",
"phoneNumber": "+2555979295712",
"lastTime": "Tue, 01 Mar 2022 13:18:23 GMT",
},
"+2555970875726":{
"uid": "I5I3SFgZT3MAxomDHrPGq17OMcO2",
"phoneNumber": "+2555970875726",
"lastTime": "Tue, 01 Mar 2022 15:43:43 GMT",
},
"+2555970125770":{
"uid": "y9IcP0S6uHcOjPk0deXbuXZC4VA3",
"phoneNumber": "+2555970125770",
"lastTime": "Tue, 25 Jan 2022 09:38:27 GMT",
},
"+2555970335754":{
"uid": "gS2RkZdBlHZNOIYQUXB1iPfOkQ33",
"phoneNumber": "+2555970335754",
"lastTime": "Fri, 18 Feb 2022 07:40:32 GMT",
},
"+2555974475787":{
"uid": "QfSNsAMob8SXYWFB8TmgC0WMAXY2",
"phoneNumber": "+2555974475787",
"lastTime": "Fri, 11 Feb 2022 08:44:47 GMT",
},
"+2555974725740":{
"uid": "9y4unCW5PHRA0AjcVZdHFXh9thy1",
"phoneNumber": "+2555974725740",
"lastTime": "Mon, 21 Feb 2022 13:57:27 GMT",
}
}
我想合并一些 phonenumber
数据,例如 +2555973055799
和 +2555974235764
,例如:它们都有带有 date
:2022-02-19
的对象,因此,合并后的结果对象应该是这样的:
{
"firstTime": "11:33:53",
"count": 149,
"date": "2022-02-19"
},
count 应该是电话号码中两个对象的总和,firstTime
应该是第一个的时间。和uid
、电话号码
应该连接起来,lastTime应该是两倍中的哪个更大?
其余的电话号码数据应该保持不变,只有这两个对象数据应该合并,
我应该怎么做?
有人可以帮忙吗?
I have some JSON Data Like This:
{
"+2555970315763":{
"uid": "u9weKk62GvXu4Yf66HM2TFXDL5S2",
"phoneNumber": "+2555970315763",
"lastTime": "Mon, 14 Feb 2022 09:40:32 GMT"
},
"+2555970605736":{
"uid": "n3qGtqMWqVXGyZMSFz7HqwtfTDB3",
"phoneNumber": "+2555970605736",
"lastTime": "Mon, 14 Feb 2022 05:32:53 GMT"
},
"+2555973055799":{
"uid": "hGHq2TLCkeWsCWOZ0KdSI6mKbvp2",
"phoneNumber": "+2555973055799",
"lastTime": "Mon, 21 Feb 2022 05:25:46 GMT",
"usage": [
{
"firstTime": "18:04:34",
"count": 1,
"date": "2022-02-19"
},
{
"firstTime": "14:53:08",
"count": 6,
"date": "2022-02-21"
},
{
"count": 4,
"date": "2022-02-22",
"firstTime": "12:53:56"
},
{
"date": "2022-02-25",
"count": 4,
"firstTime": "12:12:03"
},
{
"firstTime": "12:35:04",
"date": "2022-02-26",
"count": 34
},
{
"count": 31,
"firstTime": "11:32:03",
"date": "2022-02-27"
}
]
},
"+2555974235764":{
"uid": "uUWJvN5XVwWGfNiC6UuwlmVVTO73",
"phoneNumber": "+2555974235764",
"lastTime": "Wed, 16 Feb 2022 06:04:54 GMT",
"usage": [
{
"count": 2,
"date": "2022-02-16",
"firstTime": "12:15:38"
},
{
"date": "2022-02-17",
"firstTime": "11:45:19",
"count": 43
},
{
"count": 148,
"firstTime": "11:33:53",
"date": "2022-02-19"
},
{
"count": 127,
"firstTime": "11:42:46",
"date": "2022-02-20",
"dow": "Sunday"
},
{
"firstTime": "12:18:28",
"count": 42,
"date": "2022-02-22",
}
]
},
"+2555979295712":{
"uid": "Cx63RRHQYpMMwfAbi0ebqledk5G3",
"phoneNumber": "+2555979295712",
"lastTime": "Tue, 01 Mar 2022 13:18:23 GMT",
},
"+2555970875726":{
"uid": "I5I3SFgZT3MAxomDHrPGq17OMcO2",
"phoneNumber": "+2555970875726",
"lastTime": "Tue, 01 Mar 2022 15:43:43 GMT",
},
"+2555970125770":{
"uid": "y9IcP0S6uHcOjPk0deXbuXZC4VA3",
"phoneNumber": "+2555970125770",
"lastTime": "Tue, 25 Jan 2022 09:38:27 GMT",
},
"+2555970335754":{
"uid": "gS2RkZdBlHZNOIYQUXB1iPfOkQ33",
"phoneNumber": "+2555970335754",
"lastTime": "Fri, 18 Feb 2022 07:40:32 GMT",
},
"+2555974475787":{
"uid": "QfSNsAMob8SXYWFB8TmgC0WMAXY2",
"phoneNumber": "+2555974475787",
"lastTime": "Fri, 11 Feb 2022 08:44:47 GMT",
},
"+2555974725740":{
"uid": "9y4unCW5PHRA0AjcVZdHFXh9thy1",
"phoneNumber": "+2555974725740",
"lastTime": "Mon, 21 Feb 2022 13:57:27 GMT",
}
}
I want to merge some of the phonenumber
data like +2555973055799
and +2555974235764
, I the for example: they both have objects with date
:2022-02-19
so, the resultant object after merging should be like:
{
"firstTime": "11:33:53",
"count": 149,
"date": "2022-02-19"
},
count should be sum of both objects from phonenumbers ad firstTime
should be time which is first. and uid
, phone number
should be concatinated, lastTime should be which is greater of two times?
The remaining phonenumbers data should be unchanged, only these two objects data should be merged,
How should I do that?
can anyone please help with this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定我是否正确理解你的问题,但让我们尝试一下。
因此,您想根据使用日期合并数据。如果在同一日期使用了 2 个或更多 json 对象,那么您想查看这些数据的日期、次数和第一次使用时间?
编辑:
所以,我创建了一个原型。
也许这是一个有点复杂的解决方案,但我认为这是一个很好的开始。
我将每个对象与使用数据分开,然后我查找日期重复项,我存储了 uid 和相同的日期。然后我查找计数并比较firstTime 数据和结果变量中存储的所有内容。
检查控制台日志以获取结果。
https://jsfiddle.net/peaq6cb3/
I am not sure if i understand your question correctly, but let's give it a try.
So, you would like to merge datas according to the date of usage. If 2 or more json objects was used on the same date, then you would like to to see the date, count and first time of use of these datas?
EDIT:
So, i created a prototype.
Maybe it is a little bit complicated solution, but i think it is good for a start.
I separated every object with usage data, then i looked for date duplicates, i stored both uid and the same date. Then i looked up for the count's and compared the firstTime datas and everything stored in the result variable.
Check console log for result.
https://jsfiddle.net/peaq6cb3/