Home > OS >  How to combine common data in JSON object of Map()?
How to combine common data in JSON object of Map()?

Time:03-05

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?

CodePudding user response:

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/

  • Related