如何在Map()的JSON对象中组合通用数据?

发布于 2025-01-11 13:24:13 字数 4262 浏览 0 评论 0原文

我有一些像这样的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

请你别敷衍 2025-01-18 13:24:13

我不确定我是否正确理解你的问题,但让我们尝试一下。

因此,您想根据使用日期合并数据。如果在同一日期使用了 2 个或更多 json 对象,那么您想查看这些数据的日期、次数和第一次使用时间?

编辑:

所以,我创建了一个原型。

也许这是一个有点复杂的解决方案,但我认为这是一个很好的开始。
我将每个对象与使用数据分开,然后我查找日期重复项,我存储了 uid 和相同的日期。然后我查找计数并比较firstTime 数据和结果变量中存储的所有内容。

检查控制台日志以获取结果。

    var result = [] // we will store the result here

    var dataWithUsage = [];
    for (var i in obj) // iterate thru every data
        if (obj[i].usage)  // if data has usage property
        {
            dataWithUsage.push(obj[i]) // separate it from the main data structure
        }

    // console.log(dataWithUsage)

    var usages = [] // we will collect every usage here
    dataWithUsage.forEach(data => {
        // console.log(data.usage, data.uid)
        data.usage.forEach(element => {
            usages.push({ date: element.date, uid: data.uid })
        });
    });
    // console.log(usages)

    // we will compare every usage and we try to do it only once
    for (let i = 0; i < usages.length; i++) {
        for (let j = i + 1; j < usages.length; j++) {
            // console.log(usages[i], "vs" , usages[j])
            if (usages[i].date == usages[j].date) {
                result.push({ date: usages[i].date, uid1: usages[i].uid, uid2: usages[j].uid })
            }
        }
    }

    // at this point in the results we have our uid's with dates. Now we can do the data merge        
    result.forEach(element => {
        var uid1Index = dataWithUsage.findIndex(object => object.uid == element.uid1)
        var uid2Index = dataWithUsage.findIndex(object => object.uid == element.uid2) // we got the 2 objects for data merge

        var count = 0
        var firstTime

        let usageIndexofuid1 = dataWithUsage[uid1Index].usage.findIndex(elem => elem.date == element.date)
        let usageIndexofuid2 = dataWithUsage[uid2Index].usage.findIndex(elem => elem.date == element.date)

        count = dataWithUsage[uid1Index].usage[usageIndexofuid1].count + dataWithUsage[uid2Index].usage[usageIndexofuid2].count

        // console.log(dataWithUsage[uid1Index].usage[usageIndexofuid1].firstTime, dataWithUsage[uid2Index].usage[usageIndexofuid2].firstTime)
        // console.log(dataWithUsage[uid1Index].usage[usageIndexofuid1].firstTime > dataWithUsage[uid2Index].usage[usageIndexofuid2].firstTime)

        firstTime = ((dataWithUsage[uid1Index].usage[usageIndexofuid1].firstTime < dataWithUsage[uid2Index].usage[usageIndexofuid2].firstTime) ? dataWithUsage[uid1Index].usage[usageIndexofuid1].firstTime : dataWithUsage[uid2Index].usage[usageIndexofuid2].firstTime)

        // console.log(firstTime,"fistTime")
        // console.log(count,"count")

        element.count = count
        element.firstTime = firstTime


    });


    console.log("result")
    console.log(result)

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.

    var result = [] // we will store the result here

    var dataWithUsage = [];
    for (var i in obj) // iterate thru every data
        if (obj[i].usage)  // if data has usage property
        {
            dataWithUsage.push(obj[i]) // separate it from the main data structure
        }

    // console.log(dataWithUsage)

    var usages = [] // we will collect every usage here
    dataWithUsage.forEach(data => {
        // console.log(data.usage, data.uid)
        data.usage.forEach(element => {
            usages.push({ date: element.date, uid: data.uid })
        });
    });
    // console.log(usages)

    // we will compare every usage and we try to do it only once
    for (let i = 0; i < usages.length; i++) {
        for (let j = i + 1; j < usages.length; j++) {
            // console.log(usages[i], "vs" , usages[j])
            if (usages[i].date == usages[j].date) {
                result.push({ date: usages[i].date, uid1: usages[i].uid, uid2: usages[j].uid })
            }
        }
    }

    // at this point in the results we have our uid's with dates. Now we can do the data merge        
    result.forEach(element => {
        var uid1Index = dataWithUsage.findIndex(object => object.uid == element.uid1)
        var uid2Index = dataWithUsage.findIndex(object => object.uid == element.uid2) // we got the 2 objects for data merge

        var count = 0
        var firstTime

        let usageIndexofuid1 = dataWithUsage[uid1Index].usage.findIndex(elem => elem.date == element.date)
        let usageIndexofuid2 = dataWithUsage[uid2Index].usage.findIndex(elem => elem.date == element.date)

        count = dataWithUsage[uid1Index].usage[usageIndexofuid1].count + dataWithUsage[uid2Index].usage[usageIndexofuid2].count

        // console.log(dataWithUsage[uid1Index].usage[usageIndexofuid1].firstTime, dataWithUsage[uid2Index].usage[usageIndexofuid2].firstTime)
        // console.log(dataWithUsage[uid1Index].usage[usageIndexofuid1].firstTime > dataWithUsage[uid2Index].usage[usageIndexofuid2].firstTime)

        firstTime = ((dataWithUsage[uid1Index].usage[usageIndexofuid1].firstTime < dataWithUsage[uid2Index].usage[usageIndexofuid2].firstTime) ? dataWithUsage[uid1Index].usage[usageIndexofuid1].firstTime : dataWithUsage[uid2Index].usage[usageIndexofuid2].firstTime)

        // console.log(firstTime,"fistTime")
        // console.log(count,"count")

        element.count = count
        element.firstTime = firstTime


    });


    console.log("result")
    console.log(result)

https://jsfiddle.net/peaq6cb3/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文