phú phạm
phú phạm

Reputation: 57

I want Group By Arrays of Objects like specific array objects in JavaScript

i have array objects like here:

const mockData = [
    {
        "Id": "596840b0c09b5e6a28433715",
        "Status": 0,
        "CreatedAt": "2020-09-23T08:12:48.378Z",
        "UpdatedAt": "2020-09-23T08:12:48.378Z",
        "Name": {
            "vi": "Việt Nam",
            "en": "Vietnam"
        },
        "AppId": "",
        "ParentId": "",
        "AdminLevel": 2
    },
    {
        "Id": "596840b0c09b5e6a28434717",
        "Status": 0,
        "CreatedAt": "2020-10-16T04:13:35.548Z",
        "UpdatedAt": "2020-10-16T04:13:35.548Z",
        "Name": {
            "vi": "Thành phố Hải Phòng",
            "en": "Hai Phong City"
        },
        "AppId": "",
        "ParentId": "596840b0c09b5e6a28433715",
        "AdminLevel": 4
    },
    {
        "Id": "596840b0c09b5e6a28435d6b",
        "Status": 0,
        "CreatedAt": "2020-10-16T04:13:35.548Z",
        "UpdatedAt": "2020-10-16T04:13:35.548Z",
        "Name": {
            "vi": "Thành phố Hồ Chí Minh",
            "en": "Ho Chi Minh City"
        },
        "AppId": "",
        "ParentId": "596840b0c09b5e6a28433715",
        "AdminLevel": 4
    },
    {
        "Id": "596840b0c09b5e6a2843535e",
        "Status": 0,
        "CreatedAt": "2020-10-16T04:13:35.548Z",
        "UpdatedAt": "2020-10-16T04:13:35.548Z",
        "Name": {
            "vi": "Thành phố Đà Nẵng",
            "en": "Da Nang City"
        },
        "AppId": "",
        "ParentId": "596840b0c09b5e6a28433715",
        "AdminLevel": 4
    },
    {
        "Id": "596840b0c09b5e6a28435e82",
        "Status": 0,
        "CreatedAt": "2020-10-20T02:18:49.934Z",
        "UpdatedAt": "2020-10-20T02:18:49.934Z",
        "Name": {
            "vi": "Huyện Củ Chi",
            "en": "Cu Chi District"
        },
        "AppId": "",
        "ParentId": "596840b0c09b5e6a28435d6b",
        "AdminLevel": 6
    },
    {
        "Id": "596840b0c09b5e6a28435e98",
        "Status": 0,
        "CreatedAt": "2020-10-20T02:18:49.934Z",
        "UpdatedAt": "2020-10-20T02:18:49.934Z",
        "Name": {
            "vi": "Huyện Hóc Môn",
            "en": "Hoc Mon District"
        },
        "AppId": "",
        "ParentId": "596840b0c09b5e6a28435d6b",
        "AdminLevel": 6
    },
    {
        "Id": "596840b0c09b5e6a28435eb6",
        "Status": 0,
        "CreatedAt": "2020-10-20T02:18:49.934Z",
        "UpdatedAt": "2020-10-20T02:18:49.934Z",
        "Name": {
            "vi": "Huyện Nhà Bè",
            "en": "Nha Be District"
        },
        "AppId": "",
        "ParentId": "596840b0c09b5e6a28435d6b",
        "AdminLevel": 6
    },
    {
        "Id": "596840b0c09b5e6a28435d6c",
        "Status": 0,
        "CreatedAt": "2020-10-20T02:18:49.934Z",
        "UpdatedAt": "2020-10-20T02:18:49.934Z",
        "Name": {
            "vi": "Quận 1",
            "en": "District 1"
        },
        "AppId": "",
        "ParentId": "596840b0c09b5e6a28435d6b",
        "AdminLevel": 6
    },
    {
        "Id": "596840b0c09b5e6a2843538b",
        "Status": 0,
        "CreatedAt": "2020-10-20T02:18:50.831Z",
        "UpdatedAt": "2020-10-20T02:18:50.831Z",
        "Name": {
            "vi": "Quận Cẩm Lệ",
            "en": "Cam Le District"
        },
        "AppId": "",
        "ParentId": "596840b0c09b5e6a2843535e",
        "AdminLevel": 6
    },
    {
        "Id": "596840b0c09b5e6a28435370",
        "Status": 0,
        "CreatedAt": "2020-10-20T02:18:50.831Z",
        "UpdatedAt": "2020-10-20T02:18:50.831Z",
        "Name": {
            "vi": "Quận Hải Châu",
            "en": "Hai Chau District"
        },
        "AppId": "",
        "ParentId": "596840b0c09b5e6a2843535e",
        "AdminLevel": 6
    }
]

i want group by it become like below:

[
    {
        Id:"596840b0c09b5e6a2843535e",
        AdminLevel: 4, // demo field to understand, will remove in result
        selected: ["596840b0c09b5e6a28435370","596840b0c09b5e6a2843538b"] //array string contains Id of Adminlevel 6 follow ParentId ( is Id of AdminLevel 4 )
    },
     .....//another object with object like on top
     .....
]

Explain want:

-Group by Id Follow AdminLevel 4.

-Selected is string array Id of AdminLevel 6, have ParentId match with Id of Adminlevel 4.

I have code for this, but it still not in line with expectations of my.

let group = mockData.reduce(function (rv, x) {
            if (x.ParentId && x.AdminLevel === 6) {
                (rv[x["ParentId"]] = rv[x["ParentId"]] || []).push(x.Id);
            }
            return rv;
        }, {});

AnyOne can have new resolve or continue fix for my code? Thanks very much.

Upvotes: 0

Views: 63

Answers (1)

charlietfl
charlietfl

Reputation: 171669

Your reduce isn't doing anything with the AdminLevel 4 items.

Check the AdminLevel to determine which property to use as the group key and if the accumulator object doesn't have that key create the new object.

Then check if AdminLevel is 6 to push it's Id into the selected array

const grouped = mockData.filter(o => [4,6].includes(o.AdminLevel))
               .reduce((acc, {Id, AdminLevel, ParentId})=>{
                    const key = AdminLevel === 4 ? Id: ParentId;
                    acc[key] = acc[key] || {Id, AdminLevel:4, Selected:[]}
                    if(AdminLevel === 6){
                       acc[key].Selected.push(Id);
                    }
                    return acc;                            
                },{});

const res = Object.values(grouped)                       
console.log(res)
.as-console-wrapper {   max-height: 100%!important;top:0;}
<script>
const mockData=[{Id:"596840b0c09b5e6a28433715",Status:0,CreatedAt:"2020-09-23T08:12:48.378Z",UpdatedAt:"2020-09-23T08:12:48.378Z",Name:{vi:"Việt Nam",en:"Vietnam"},AppId:"",ParentId:"",AdminLevel:2},{Id:"596840b0c09b5e6a28434717",Status:0,CreatedAt:"2020-10-16T04:13:35.548Z",UpdatedAt:"2020-10-16T04:13:35.548Z",Name:{vi:"Thành phố Hải Phòng",en:"Hai Phong City"},AppId:"",ParentId:"596840b0c09b5e6a28433715",AdminLevel:4},{Id:"596840b0c09b5e6a28435d6b",Status:0,CreatedAt:"2020-10-16T04:13:35.548Z",UpdatedAt:"2020-10-16T04:13:35.548Z",Name:{vi:"Thành phố Hồ Chí Minh",en:"Ho Chi Minh City"},AppId:"",ParentId:"596840b0c09b5e6a28433715",AdminLevel:4},{Id:"596840b0c09b5e6a2843535e",Status:0,CreatedAt:"2020-10-16T04:13:35.548Z",UpdatedAt:"2020-10-16T04:13:35.548Z",Name:{vi:"Thành phố Đà Nẵng",en:"Da Nang City"},AppId:"",ParentId:"596840b0c09b5e6a28433715",AdminLevel:4},{Id:"596840b0c09b5e6a28435e82",Status:0,CreatedAt:"2020-10-20T02:18:49.934Z",UpdatedAt:"2020-10-20T02:18:49.934Z",Name:{vi:"Huyện Củ Chi",en:"Cu Chi District"},AppId:"",ParentId:"596840b0c09b5e6a28435d6b",AdminLevel:6},{Id:"596840b0c09b5e6a28435e98",Status:0,CreatedAt:"2020-10-20T02:18:49.934Z",UpdatedAt:"2020-10-20T02:18:49.934Z",Name:{vi:"Huyện Hóc Môn",en:"Hoc Mon District"},AppId:"",ParentId:"596840b0c09b5e6a28435d6b",AdminLevel:6},{Id:"596840b0c09b5e6a28435eb6",Status:0,CreatedAt:"2020-10-20T02:18:49.934Z",UpdatedAt:"2020-10-20T02:18:49.934Z",Name:{vi:"Huyện Nhà Bè",en:"Nha Be District"},AppId:"",ParentId:"596840b0c09b5e6a28435d6b",AdminLevel:6},{Id:"596840b0c09b5e6a28435d6c",Status:0,CreatedAt:"2020-10-20T02:18:49.934Z",UpdatedAt:"2020-10-20T02:18:49.934Z",Name:{vi:"Quận 1",en:"District 1"},AppId:"",ParentId:"596840b0c09b5e6a28435d6b",AdminLevel:6},{Id:"596840b0c09b5e6a2843538b",Status:0,CreatedAt:"2020-10-20T02:18:50.831Z",UpdatedAt:"2020-10-20T02:18:50.831Z",Name:{vi:"Quận Cẩm Lệ",en:"Cam Le District"},AppId:"",ParentId:"596840b0c09b5e6a2843535e",AdminLevel:6},{Id:"596840b0c09b5e6a28435370",Status:0,CreatedAt:"2020-10-20T02:18:50.831Z",UpdatedAt:"2020-10-20T02:18:50.831Z",Name:{vi:"Quận Hải Châu",en:"Hai Chau District"},AppId:"",ParentId:"596840b0c09b5e6a2843535e",AdminLevel:6}];
</script>

Upvotes: 1

Related Questions