How to grade a normal array according to a field and reorganize it into a composite array?

now there is an array of multi-level dots, and you want to reorganize the array according to the dot level level field in this array, with the goal of reassembling a composite array.
the original array code is as follows:

list: [
            {
                "id": 30064771072,
                "lastModified": "2019-02-13 20:14:42:000",
                "userCount": 4,
                "deviceCount": 4,
                "level": 0,
                "name": "11"
            },
            {
                "id": 30081548288,
                "lastModified": "2019-01-30 14:36:33:729",
                "userCount": 0,
                "deviceCount": 0,
                "level": 1,
                "name": ""
            },
            {
                "id": 30081679360,
                "lastModified": "2019-01-30 14:23:33:345",
                "userCount": 0,
                "deviceCount": 0,
                "level": 2,
                "name": "120"
            },
            {
                "id": 30081744896,
                "lastModified": "2019-01-30 06:52:07:915",
                "userCount": 0,
                "deviceCount": 0,
                "level": 2,
                "name": "360"
            },
            {
                "id": 30098325504,
                "lastModified": "2019-01-29 09:52:27:640",
                "userCount": 0,
                "deviceCount": 0,
                "level": 1,
                "name": "120"
            },
            {
                "id": 30131879936,
                "lastModified": "2019-01-29 10:14:03:897",
                "userCount": 0,
                "deviceCount": 0,
                "level": 1,
                "name": ""
            },
            {
                "id": 30148657152,
                "lastModified": "2019-01-30 02:44:48:868",
                "userCount": 0,
                "deviceCount": 0,
                "level": 1,
                "name": ""
            },
            {
                "id": 30165434368,
                "lastModified": "2019-01-30 14:39:28:452",
                "userCount": 0,
                "deviceCount": 0,
                "level": 1,
                "name": ""
            },
            {
                "id": 30182211584,
                "lastModified": "2019-01-30 15:32:40:672",
                "userCount": 0,
                "deviceCount": 0,
                "level": 1,
                "name": ""
            },
            {
                "id": 30215766016,
                "lastModified": "2019-01-30 15:33:17:230",
                "userCount": 0,
                "deviceCount": 0,
                "level": 1,
                "name": ""
            },
            {
                "id": 30232543232,
                "lastModified": "2019-01-30 07:35:41:689",
                "userCount": 0,
                "deviceCount": 0,
                "level": 1,
                "name": "77"
            },
            {
                "id": 30249320448,
                "lastModified": "2019-01-30 08:30:30:981",
                "userCount": 0,
                "deviceCount": 0,
                "level": 1,
                "name": "33344"
            },

        ],

level is a site rating field, which has four levels: 0meme, 1meme, 2pje 3. The target array is based on this rating
the target array code is as follows:

listBox:[{//
            id:"",
            name:"",
            lastModified:"",
            userCount:"",
            deviceCount:"",

            children:[{ //
                    id:"",
                    name:"",
                    lastModified:"",
                    userCount:"",
                    deviceCount:"",

                    maxList:[ {//
                        id:"",
                        name:"",
                        lastModified:"",
                        userCount:"",
                        deviceCount:"",   

                        minList:[]//
                        
                    }]                   
            }]
        }],  

I originally intended to use the for loop to nest the if implementation, but the implementation was modified several times
and the final implementation code is as follows:

 for(let i=0;i<this.list.length;iPP){//
                if(this.list[i].level==0){//1
                        this.listBox[i].children = "";
                    }else if(this.list[i].level==1){//2
                        this.listBox[i].children[i].maxList = this.list[i];
                        this.listBox[i].children[i].mediumList = "",
                        this.listBox[i].children[i].minList = ""
                    }else if(this.list[i].level==2){//3
                        this.listBox[i].children[i].mediumList =this.list[i];
                        this.listBox[i].children[i].minList = ""
                    }else {//4
                        this.listBox[i].children[i].minList = this.list[i];
                    }
                    this.listBox[i].id = this.list[i].id;
                    this.listBox[i].name = this.list[i].name;
                    this.listBox[i].lastModified = this.list[i].lastModified;
                    this.listBox[i].userCount = this.list[i].userCount;
                    this.listBox[i].deviceCount = this.list[i].deviceCount;
               
            }
            console.log(this.listBox);

however, my method gets cold when it doesn"t get to console.log. And my method is not efficient, each time the page is loaded, I have to traverse all the data in the for loop. I hope all the great gods can teach us an efficient and feasible method. Thank you very much!

Jun.16,2022

generally, there are two id, one is your own id, and the other is the parent id. To merge in this way.

what you have now is not interesting. It is not suitable for your kind of structure.

fits the following structure. Press level , push to different arrays.

{
    0: [{}],
    1: [{},{}],
    2: [{},{}],
    3: [{},{}],
}
< hr >

you can then iterate through the above structure, adding 1 to matList of 0 [0].


according to what you said upstairs:
level=0, how many are there? If there is only one, you can put the screened level=1 push in.
level=1, how many? Then correspond the data of level=2 to the data of level=1 according to that field.
it doesn't fit the structure you want


try this
clipboard.png

Menu