[
{
"_id": "615d40a21503e000147d4e97",
"zone": "A",
"country": "India",
"operator_alias": "Idea",
"network_code": "2590",
"increment_type": "KB",
"network_operator": "615d40a21503e000147d4e96",
"network_category": "M2M",
"updated_at": "2021-10-06T06:22:26.653Z",
"__v": 0
},
{
"_id": "615d40a21503e000147d4e98",
"zone": "A",
"country": "India",
"operator_alias": "Idea",
"network_code": "2591",
"increment_type": "KB",
"network_operator": "615d40a21503e000147d4e96",
"network_category": "M2M",
"updated_at": "2021-10-06T06:22:26.654Z",
"__v": 0
},
{
"_id": "615d40a21503e000147d4e99",
"zone": "A",
"country": "India",
"operator_alias": "Idea",
"network_code": "2592",
"increment_type": "KB",
"network_operator": "615d40a21503e000147d4e96",
"network_category": "M2M",
"updated_at": "2021-10-06T06:22:26.654Z",
"__v": 0
},
{
"_id": "615d40a21503e000147d4e9a",
"zone": "A",
"country": "India",
"operator_alias": "Idea",
"network_code": "2593",
"increment_type": "KB",
"network_operator": "615d40a21503e000147d4e96",
"network_category": "M2M",
"updated_at": "2021-10-06T06:22:26.654Z",
"__v": 0
},
{
"_id": "615d40a21503e000147d4e9b",
"zone": "A",
"country": "India",
"operator_alias": "Airtel",
"network_code": "2594",
"increment_type": "KB",
"network_operator": "615d40a21503e000147d4e96",
"network_category": "M2M",
"updated_at": "2021-10-06T06:22:26.654Z",
"__v": 0
},
{
"_id": "615d40a21503e000147d4e9c",
"zone": "A",
"country": "India",
"operator_alias": "Airtel",
"network_code": "2595",
"increment_type": "KB",
"network_operator": "615d40a21503e000147d4e96",
"network_category": "M2M",
"updated_at": "2021-10-06T06:22:26.654Z",
"__v": 0
},
{
"_id": "615d40a21503e000147d4e9d",
"zone": "A",
"country": "India",
"operator_alias": "Airtel",
"network_code": "2596",
"increment_type": "KB",
"network_operator": "615d40a21503e000147d4e96",
"network_category": "M2M",
"updated_at": "2021-10-06T06:22:26.654Z",
"__v": 0
},
{
"_id": "615d40a21503e000147d4e9e",
"zone": "A",
"country": "India",
"operator_alias": "Jio",
"network_code": "2597",
"increment_type": "KB",
"network_operator": "615d40a21503e000147d4e96",
"network_category": "M2M",
"updated_at": "2021-10-06T06:22:26.654Z",
"__v": 0
},
{
"_id": "615d40a21503e000147d4e9f",
"zone": "A",
"country": "India",
"operator_alias": "Jio",
"network_code": "2598",
"increment_type": "KB",
"network_operator": "615d40a21503e000147d4e96",
"network_category": "M2M",
"updated_at": "2021-10-06T06:22:26.654Z",
"__v": 0
},
{
"_id": "615d40a21503e000147d4ea0",
"zone": "A",
"country": "India",
"operator_alias": "Jio",
"network_code": "2599",
"increment_type": "KB",
"network_operator": "615d40a21503e000147d4e96",
"network_category": "M2M",
"updated_at": "2021-10-06T06:22:26.654Z",
"__v": 0
},
{
"_id": "615d40a21503e000147d4ea1",
"zone": "A",
"country": "China",
"operator_alias": "Idea",
"network_code": "2580",
"increment_type": "KB",
"network_operator": "615d40a21503e000147d4e96",
"network_category": "M2M",
"updated_at": "2021-10-06T06:22:26.654Z",
"__v": 0
},
{
"_id": "615d40a21503e000147d4ea2",
"zone": "A",
"country": "China",
"operator_alias": "Idea",
"network_code": "2581",
"increment_type": "KB",
"network_operator": "615d40a21503e000147d4e96",
"network_category": "M2M",
"updated_at": "2021-10-06T06:22:26.655Z",
"__v": 0
},
{
"_id": "615d40a21503e000147d4ea3",
"zone": "A",
"country": "China",
"operator_alias": "Airtel",
"network_code": "2582",
"increment_type": "KB",
"network_operator": "615d40a21503e000147d4e96",
"network_category": "M2M",
"updated_at": "2021-10-06T06:22:26.655Z",
"__v": 0
},
{
"_id": "615d40a21503e000147d4ea4",
"zone": "A",
"country": "China",
"operator_alias": "Airtel",
"network_code": "2583",
"increment_type": "KB",
"network_operator": "615d40a21503e000147d4e96",
"network_category": "M2M",
"updated_at": "2021-10-06T06:22:26.655Z",
"__v": 0
},
{
"_id": "615d40a21503e000147d4ea5",
"zone": "A",
"country": "China",
"operator_alias": "Jio",
"network_code": "2584",
"increment_type": "KB",
"network_operator": "615d40a21503e000147d4e96",
"network_category": "M2M",
"updated_at": "2021-10-06T06:22:26.655Z",
"__v": 0
},
{
"_id": "615d40a21503e000147d4ea6",
"zone": "A",
"country": "China",
"operator_alias": "Jio",
"network_code": "2585",
"increment_type": "KB",
"network_operator": "615d40a21503e000147d4e96",
"network_category": "M2M",
"updated_at": "2021-10-06T06:22:26.655Z",
"__v": 0
},
{
"_id": "615d40a21503e000147d4ea7",
"zone": "A",
"country": "France",
"operator_alias": "Idea",
"network_code": "2586",
"increment_type": "KB",
"network_operator": "615d40a21503e000147d4e96",
"network_category": "M2M",
"updated_at": "2021-10-06T06:22:26.655Z",
"__v": 0
},
{
"_id": "615d40a21503e000147d4ea8",
"zone": "A",
"country": "France",
"operator_alias": "Idea",
"network_code": "2587",
"increment_type": "KB",
"network_operator": "615d40a21503e000147d4e96",
"network_category": "M2M",
"updated_at": "2021-10-06T06:22:26.656Z",
"__v": 0
},
{
"_id": "615d40a21503e000147d4ea9",
"zone": "A",
"country": "France",
"operator_alias": "Airtel",
"network_code": "2588",
"increment_type": "KB",
"network_operator": "615d40a21503e000147d4e96",
"network_category": "M2M",
"updated_at": "2021-10-06T06:22:26.656Z",
"__v": 0
},
{
"_id": "615d40a21503e000147d4eaa",
"zone": "A",
"country": "France",
"operator_alias": "Airtel",
"network_code": "2589",
"increment_type": "KB",
"network_operator": "615d40a21503e000147d4e96",
"network_category": "M2M",
"updated_at": "2021-10-06T06:22:26.656Z",
"__v": 0
},
{
"_id": "615d40a21503e000147d4eab",
"zone": "A",
"country": "France",
"operator_alias": "Jio",
"network_code": "2570",
"increment_type": "KB",
"network_operator": "615d40a21503e000147d4e96",
"network_category": "M2M",
"updated_at": "2021-10-06T06:22:26.656Z",
"__v": 0
},
{
"_id": "615d40a21503e000147d4eac",
"zone": "A",
"country": "France",
"operator_alias": "Jio",
"network_code": "2571",
"increment_type": "KB",
"network_operator": "615d40a21503e000147d4e96",
"network_category": "M2M",
"updated_at": "2021-10-06T06:22:26.657Z",
"__v": 0
},
{
"_id": "615d40a21503e000147d4ead",
"zone": "B",
"country": "India",
"operator_alias": "Idea",
"network_code": "2572",
"increment_type": "KB",
"network_operator": "615d40a21503e000147d4e96",
"network_category": "M2M",
"updated_at": "2021-10-06T06:22:26.657Z",
"__v": 0
},
{
"_id": "615d40a21503e000147d4eae",
"zone": "B",
"country": "India",
"operator_alias": "Idea",
"network_code": "2573",
"increment_type": "KB",
"network_operator": "615d40a21503e000147d4e96",
"network_category": "M2M",
"updated_at": "2021-10-06T06:22:26.657Z",
"__v": 0
},
{
"_id": "615d40a21503e000147d4eaf",
"zone": "B",
"country": "India",
"operator_alias": "Idea",
"network_code": "2574",
"increment_type": "KB",
"network_operator": "615d40a21503e000147d4e96",
"network_category": "M2M",
"updated_at": "2021-10-06T06:22:26.657Z",
"__v": 0
},
{
"_id": "615d40a21503e000147d4eb0",
"zone": "B",
"country": "China",
"operator_alias": "Airtel",
"network_code": "2575",
"increment_type": "KB",
"network_operator": "615d40a21503e000147d4e96",
"network_category": "M2M",
"updated_at": "2021-10-06T06:22:26.657Z",
"__v": 0
},
{
"_id": "615d40a21503e000147d4eb1",
"zone": "B",
"country": "China",
"operator_alias": "Airtel",
"network_code": "2576",
"increment_type": "KB",
"network_operator": "615d40a21503e000147d4e96",
"network_category": "M2M",
"updated_at": "2021-10-06T06:22:26.657Z",
"__v": 0
},
{
"_id": "615d40a21503e000147d4eb2",
"zone": "B",
"country": "China",
"operator_alias": "Airtel",
"network_code": "2577",
"increment_type": "KB",
"network_operator": "615d40a21503e000147d4e96",
"network_category": "M2M",
"updated_at": "2021-10-06T06:22:26.657Z",
"__v": 0
},
{
"_id": "615d40a21503e000147d4eb3",
"zone": "B",
"country": "India",
"operator_alias": "Airtel",
"network_code": "2578",
"increment_type": "KB",
"network_operator": "615d40a21503e000147d4e96",
"network_category": "M2M",
"updated_at": "2021-10-06T06:22:26.658Z",
"__v": 0
},
{
"_id": "615d40a21503e000147d4eb4",
"zone": "B",
"country": "India",
"operator_alias": "Jio",
"network_code": "2579",
"increment_type": "KB",
"network_operator": "615d40a21503e000147d4e96",
"network_category": "M2M",
"updated_at": "2021-10-06T06:22:26.658Z",
"__v": 0
},
{
"_id": "615d40a21503e000147d4eb5",
"zone": "B",
"country": "India",
"operator_alias": "Jio",
"network_code": "2560",
"increment_type": "KB",
"network_operator": "615d40a21503e000147d4e96",
"network_category": "M2M",
"updated_at": "2021-10-06T06:22:26.658Z",
"__v": 0
},
{
"_id": "615d40a21503e000147d4eb6",
"zone": "B",
"country": "India",
"operator_alias": "Jio",
"network_code": "2561",
"increment_type": "KB",
"network_operator": "615d40a21503e000147d4e96",
"network_category": "M2M",
"updated_at": "2021-10-06T06:22:26.658Z",
"__v": 0
},
{
"_id": "615d40a21503e000147d4eb7",
"zone": "B",
"country": "China",
"operator_alias": "Idea",
"network_code": "2562",
"increment_type": "KB",
"network_operator": "615d40a21503e000147d4e96",
"network_category": "M2M",
"updated_at": "2021-10-06T06:22:26.658Z",
"__v": 0
},
{
"_id": "615d40a21503e000147d4eb8",
"zone": "B",
"country": "China",
"operator_alias": "Idea",
"network_code": "2563",
"increment_type": "KB",
"network_operator": "615d40a21503e000147d4e96",
"network_category": "M2M",
"updated_at": "2021-10-06T06:22:26.658Z",
"__v": 0
},
{
"_id": "615d40a21503e000147d4eb9",
"zone": "C",
"country": "India",
"operator_alias": "Idea",
"network_code": "2564",
"increment_type": "KB",
"network_operator": "615d40a21503e000147d4e96",
"network_category": "M2M",
"updated_at": "2021-10-06T06:22:26.658Z",
"__v": 0
},
{
"_id": "615d40a21503e000147d4eba",
"zone": "C",
"country": "India",
"operator_alias": "Idea",
"network_code": "2565",
"increment_type": "KB",
"network_operator": "615d40a21503e000147d4e96",
"network_category": "M2M",
"updated_at": "2021-10-06T06:22:26.659Z",
"__v": 0
},
{
"_id": "615d40a21503e000147d4ebb",
"zone": "C",
"country": "India",
"operator_alias": "Idea",
"network_code": "2566",
"increment_type": "KB",
"network_operator": "615d40a21503e000147d4e96",
"network_category": "M2M",
"updated_at": "2021-10-06T06:22:26.659Z",
"__v": 0
},
{
"_id": "615d40a21503e000147d4ebc",
"zone": "C",
"country": "India",
"operator_alias": "Airtel",
"network_code": "2567",
"increment_type": "KB",
"network_operator": "615d40a21503e000147d4e96",
"network_category": "M2M",
"updated_at": "2021-10-06T06:22:26.659Z",
"__v": 0
},
{
"_id": "615d40a21503e000147d4ebd",
"zone": "C",
"country": "India",
"operator_alias": "Airtel",
"network_code": "2568",
"increment_type": "KB",
"network_operator": "615d40a21503e000147d4e96",
"network_category": "M2M",
"updated_at": "2021-10-06T06:22:26.659Z",
"__v": 0
},
{
"_id": "615d40a21503e000147d4ebe",
"zone": "C",
"country": "India",
"operator_alias": "Jio",
"network_code": "2569",
"increment_type": "KB",
"network_operator": "615d40a21503e000147d4e96",
"network_category": "M2M",
"updated_at": "2021-10-06T06:22:26.659Z",
"__v": 0
},
{
"_id": "615d40a21503e000147d4ebf",
"zone": "C",
"country": "China",
"operator_alias": "Idea",
"network_code": "2550",
"increment_type": "KB",
"network_operator": "615d40a21503e000147d4e96",
"network_category": "M2M",
"updated_at": "2021-10-06T06:22:26.659Z",
"__v": 0
},
{
"_id": "615d40a21503e000147d4ec0",
"zone": "C",
"country": "China",
"operator_alias": "Idea",
"network_code": "2551",
"increment_type": "KB",
"network_operator": "615d40a21503e000147d4e96",
"network_category": "M2M",
"updated_at": "2021-10-06T06:22:26.659Z",
"__v": 0
},
{
"_id": "615d40a21503e000147d4ec1",
"zone": "C",
"country": "China",
"operator_alias": "Airtel",
"network_code": "2552",
"increment_type": "KB",
"network_operator": "615d40a21503e000147d4e96",
"network_category": "M2M",
"updated_at": "2021-10-06T06:22:26.660Z",
"__v": 0
},
{
"_id": "615d40a21503e000147d4ec2",
"zone": "C",
"country": "China",
"operator_alias": "Airtel",
"network_code": "2553",
"increment_type": "KB",
"network_operator": "615d40a21503e000147d4e96",
"network_category": "M2M",
"updated_at": "2021-10-06T06:22:26.660Z",
"__v": 0
},
{
"_id": "615d40a21503e000147d4ec3",
"zone": "C",
"country": "China",
"operator_alias": "Jio",
"network_code": "2554",
"increment_type": "KB",
"network_operator": "615d40a21503e000147d4e96",
"network_category": "M2M",
"updated_at": "2021-10-06T06:22:26.660Z",
"__v": 0
},
{
"_id": "615d40a21503e000147d4ec4",
"zone": "C",
"country": "China",
"operator_alias": "Jio",
"network_code": "2555",
"increment_type": "KB",
"network_operator": "615d40a21503e000147d4e96",
"network_category": "M2M",
"updated_at": "2021-10-06T06:22:26.660Z",
"__v": 0
},
{
"_id": "615d40a21503e000147d4ec5",
"zone": "C",
"country": "Usa",
"operator_alias": "Idea",
"network_code": "2556",
"increment_type": "KB",
"network_operator": "615d40a21503e000147d4e96",
"network_category": "M2M",
"updated_at": "2021-10-06T06:22:26.660Z",
"__v": 0
},
{
"_id": "615d40a21503e000147d4ec6",
"zone": "C",
"country": "Usa",
"operator_alias": "Idea",
"network_code": "2557",
"increment_type": "KB",
"network_operator": "615d40a21503e000147d4e96",
"network_category": "M2M",
"updated_at": "2021-10-06T06:22:26.660Z",
"__v": 0
},
{
"_id": "615d40a21503e000147d4ec7",
"zone": "C",
"country": "Usa",
"operator_alias": "Airtel",
"network_code": "2558",
"increment_type": "KB",
"network_operator": "615d40a21503e000147d4e96",
"network_category": "M2M",
"updated_at": "2021-10-06T06:22:26.660Z",
"__v": 0
},
{
"_id": "615d40a21503e000147d4ec8",
"zone": "C",
"country": "Usa",
"operator_alias": "Airtel",
"network_code": "2559",
"increment_type": "KB",
"network_operator": "615d40a21503e000147d4e96",
"network_category": "M2M",
"updated_at": "2021-10-06T06:22:26.661Z",
"__v": 0
}
];
This is my Array of Objects Coming From Backend. I need to re-arrange or re structure this converted into below format
[
"zone":"A",
"Zone_Price":"0",
"Active":"true",
"countries":[
{
"country_name":"India",
"active":"true",
"
- List item
operators":[
{
"operator_name":"Idea",
"active":"true",
"operator_code":[
{
"operator_code":"2591",
"active":"true"
}
]
}
]
}
]
];
I need to group all zones without duplicates, suppose Zone 'A'
have multiple country, all country listed if country without duplication, inside this country have multiple operators that should be listed, inside this operators multiple codes are listed
CodePudding user response:
So this doesn't result in exactly the format you have described but should get you on your way down a path.
const raw = [{_id: "615d40a21503e000147d4e97",zone: "A",country: "India",operator_alias: "Idea",network_code: "2590",increment_type: "KB",network_operator: "615d40a21503e000147d4e96",network_category: "M2M",updated_at: "2021-10-06T06:22:26.653Z",__v: 0},{_id: "615d40a21503e000147d4e98",zone: "A",country: "India",operator_alias: "Idea",network_code: "2591",increment_type: "KB",network_operator: "615d40a21503e000147d4e96",network_category: "M2M",updated_at: "2021-10-06T06:22:26.654Z",__v: 0},{_id: "615d40a21503e000147d4e99",zone: "A",country: "India",operator_alias: "Idea",network_code: "2592",increment_type: "KB",network_operator: "615d40a21503e000147d4e96",network_category: "M2M",updated_at: "2021-10-06T06:22:26.654Z",__v: 0},{_id: "615d40a21503e000147d4e9a",zone: "A",country: "India",operator_alias: "Idea",network_code: "2593",increment_type: "KB",network_operator: "615d40a21503e000147d4e96",network_category: "M2M",updated_at: "2021-10-06T06:22:26.654Z",__v: 0},{_id: "615d40a21503e000147d4e9b",zone: "A",country: "India",operator_alias: "Airtel",network_code: "2594",increment_type: "KB",network_operator: "615d40a21503e000147d4e96",network_category: "M2M",updated_at: "2021-10-06T06:22:26.654Z",__v: 0},{_id: "615d40a21503e000147d4e9c",zone: "A",country: "India",operator_alias: "Airtel",network_code: "2595",increment_type: "KB",network_operator: "615d40a21503e000147d4e96",network_category: "M2M",updated_at: "2021-10-06T06:22:26.654Z",__v: 0},{_id: "615d40a21503e000147d4e9d",zone: "A",country: "India",operator_alias: "Airtel",network_code: "2596",increment_type: "KB",network_operator: "615d40a21503e000147d4e96",network_category: "M2M",updated_at: "2021-10-06T06:22:26.654Z",__v: 0},{_id: "615d40a21503e000147d4e9e",zone: "A",country: "India",operator_alias: "Jio",network_code: "2597",increment_type: "KB",network_operator: "615d40a21503e000147d4e96",network_category: "M2M",updated_at: "2021-10-06T06:22:26.654Z",__v: 0},{_id: "615d40a21503e000147d4e9f",zone: "A",country: "India",operator_alias: "Jio",network_code: "2598",increment_type: "KB",network_operator: "615d40a21503e000147d4e96",network_category: "M2M",updated_at: "2021-10-06T06:22:26.654Z",__v: 0},{_id: "615d40a21503e000147d4ea0",zone: "A",country: "India",operator_alias: "Jio",network_code: "2599",increment_type: "KB",network_operator: "615d40a21503e000147d4e96",network_category: "M2M",updated_at: "2021-10-06T06:22:26.654Z",__v: 0},{_id: "615d40a21503e000147d4ea1",zone: "A",country: "China",operator_alias: "Idea",network_code: "2580",increment_type: "KB",network_operator: "615d40a21503e000147d4e96",network_category: "M2M",updated_at: "2021-10-06T06:22:26.654Z",__v: 0},{_id: "615d40a21503e000147d4ea2",zone: "A",country: "China",operator_alias: "Idea",network_code: "2581",increment_type: "KB",network_operator: "615d40a21503e000147d4e96",network_category: "M2M",updated_at: "2021-10-06T06:22:26.655Z",__v: 0},{_id: "615d40a21503e000147d4ea3",zone: "A",country: "China",operator_alias: "Airtel",network_code: "2582",increment_type: "KB",network_operator: "615d40a21503e000147d4e96",network_category: "M2M",updated_at: "2021-10-06T06:22:26.655Z",__v: 0},{_id: "615d40a21503e000147d4ea4",zone: "A",country: "China",operator_alias: "Airtel",network_code: "2583",increment_type: "KB",network_operator: "615d40a21503e000147d4e96",network_category: "M2M",updated_at: "2021-10-06T06:22:26.655Z",__v: 0},{_id: "615d40a21503e000147d4ea5",zone: "A",country: "China",operator_alias: "Jio",network_code: "2584",increment_type: "KB",network_operator: "615d40a21503e000147d4e96",network_category: "M2M",updated_at: "2021-10-06T06:22:26.655Z",__v: 0},{_id: "615d40a21503e000147d4ea6",zone: "A",country: "China",operator_alias: "Jio",network_code: "2585",increment_type: "KB",network_operator: "615d40a21503e000147d4e96",network_category: "M2M",updated_at: "2021-10-06T06:22:26.655Z",__v: 0},{_id: "615d40a21503e000147d4ea7",zone: "A",country: "France",operator_alias: "Idea",network_code: "2586",increment_type: "KB",network_operator: "615d40a21503e000147d4e96",network_category: "M2M",updated_at: "2021-10-06T06:22:26.655Z",__v: 0},{_id: "615d40a21503e000147d4ea8",zone: "A",country: "France",operator_alias: "Idea",network_code: "2587",increment_type: "KB",network_operator: "615d40a21503e000147d4e96",network_category: "M2M",updated_at: "2021-10-06T06:22:26.656Z",__v: 0},{_id: "615d40a21503e000147d4ea9",zone: "A",country: "France",operator_alias: "Airtel",network_code: "2588",increment_type: "KB",network_operator: "615d40a21503e000147d4e96",network_category: "M2M",updated_at: "2021-10-06T06:22:26.656Z",__v: 0},{_id: "615d40a21503e000147d4eaa",zone: "A",country: "France",operator_alias: "Airtel",network_code: "2589",increment_type: "KB",network_operator: "615d40a21503e000147d4e96",network_category: "M2M",updated_at: "2021-10-06T06:22:26.656Z",__v: 0},{_id: "615d40a21503e000147d4eab",zone: "A",country: "France",operator_alias: "Jio",network_code: "2570",increment_type: "KB",network_operator: "615d40a21503e000147d4e96",network_category: "M2M",updated_at: "2021-10-06T06:22:26.656Z",__v: 0},{_id: "615d40a21503e000147d4eac",zone: "A",country: "France",operator_alias: "Jio",network_code: "2571",increment_type: "KB",network_operator: "615d40a21503e000147d4e96",network_category: "M2M",updated_at: "2021-10-06T06:22:26.657Z",__v: 0},{_id: "615d40a21503e000147d4ead",zone: "B",country: "India",operator_alias: "Idea",network_code: "2572",increment_type: "KB",network_operator: "615d40a21503e000147d4e96",network_category: "M2M",updated_at: "2021-10-06T06:22:26.657Z",__v: 0},{_id: "615d40a21503e000147d4eae",zone: "B",country: "India",operator_alias: "Idea",network_code: "2573",increment_type: "KB",network_operator: "615d40a21503e000147d4e96",network_category: "M2M",updated_at: "2021-10-06T06:22:26.657Z",__v: 0},{_id: "615d40a21503e000147d4eaf",zone: "B",country: "India",operator_alias: "Idea",network_code: "2574",increment_type: "KB",network_operator: "615d40a21503e000147d4e96",network_category: "M2M",updated_at: "2021-10-06T06:22:26.657Z",__v: 0},{_id: "615d40a21503e000147d4eb0",zone: "B",country: "China",operator_alias: "Airtel",network_code: "2575",increment_type: "KB",network_operator: "615d40a21503e000147d4e96",network_category: "M2M",updated_at: "2021-10-06T06:22:26.657Z",__v: 0},{_id: "615d40a21503e000147d4eb1",zone: "B",country: "China",operator_alias: "Airtel",network_code: "2576",increment_type: "KB",network_operator: "615d40a21503e000147d4e96",network_category: "M2M",updated_at: "2021-10-06T06:22:26.657Z",__v: 0},{_id: "615d40a21503e000147d4eb2",zone: "B",country: "China",operator_alias: "Airtel",network_code: "2577",increment_type: "KB",network_operator: "615d40a21503e000147d4e96",network_category: "M2M",updated_at: "2021-10-06T06:22:26.657Z",__v: 0},{_id: "615d40a21503e000147d4eb3",zone: "B",country: "India",operator_alias: "Airtel",network_code: "2578",increment_type: "KB",network_operator: "615d40a21503e000147d4e96",network_category: "M2M",updated_at: "2021-10-06T06:22:26.658Z",__v: 0},{_id: "615d40a21503e000147d4eb4",zone: "B",country: "India",operator_alias: "Jio",network_code: "2579",increment_type: "KB",network_operator: "615d40a21503e000147d4e96",network_category: "M2M",updated_at: "2021-10-06T06:22:26.658Z",__v: 0},{_id: "615d40a21503e000147d4eb5",zone: "B",country: "India",operator_alias: "Jio",network_code: "2560",increment_type: "KB",network_operator: "615d40a21503e000147d4e96",network_category: "M2M",updated_at: "2021-10-06T06:22:26.658Z",__v: 0},{_id: "615d40a21503e000147d4eb6",zone: "B",country: "India",operator_alias: "Jio",network_code: "2561",increment_type: "KB",network_operator: "615d40a21503e000147d4e96",network_category: "M2M",updated_at: "2021-10-06T06:22:26.658Z",__v: 0},{_id: "615d40a21503e000147d4eb7",zone: "B",country: "China",operator_alias: "Idea",network_code: "2562",increment_type: "KB",network_operator: "615d40a21503e000147d4e96",network_category: "M2M",updated_at: "2021-10-06T06:22:26.658Z",__v: 0},{_id: "615d40a21503e000147d4eb8",zone: "B",country: "China",operator_alias: "Idea",network_code: "2563",increment_type: "KB",network_operator: "615d40a21503e000147d4e96",network_category: "M2M",updated_at: "2021-10-06T06:22:26.658Z",__v: 0},{_id: "615d40a21503e000147d4eb9",zone: "C",country: "India",operator_alias: "Idea",network_code: "2564",increment_type: "KB",network_operator: "615d40a21503e000147d4e96",network_category: "M2M",updated_at: "2021-10-06T06:22:26.658Z",__v: 0},{_id: "615d40a21503e000147d4eba",zone: "C",country: "India",operator_alias: "Idea",network_code: "2565",increment_type: "KB",network_operator: "615d40a21503e000147d4e96",network_category: "M2M",updated_at: "2021-10-06T06:22:26.659Z",__v: 0},{_id: "615d40a21503e000147d4ebb",zone: "C",country: "India",operator_alias: "Idea",network_code: "2566",increment_type: "KB",network_operator: "615d40a21503e000147d4e96",network_category: "M2M",updated_at: "2021-10-06T06:22:26.659Z",__v: 0},{_id: "615d40a21503e000147d4ebc",zone: "C",country: "India",operator_alias: "Airtel",network_code: "2567",increment_type: "KB",network_operator: "615d40a21503e000147d4e96",network_category: "M2M",updated_at: "2021-10-06T06:22:26.659Z",__v: 0},{_id: "615d40a21503e000147d4ebd",zone: "C",country: "India",operator_alias: "Airtel",network_code: "2568",increment_type: "KB",network_operator: "615d40a21503e000147d4e96",network_category: "M2M",updated_at: "2021-10-06T06:22:26.659Z",__v: 0},{_id: "615d40a21503e000147d4ebe",zone: "C",country: "India",operator_alias: "Jio",network_code: "2569",increment_type: "KB",network_operator: "615d40a21503e000147d4e96",network_category: "M2M",updated_at: "2021-10-06T06:22:26.659Z",__v: 0},{_id: "615d40a21503e000147d4ebf",zone: "C",country: "China",operator_alias: "Idea",network_code: "2550",increment_type: "KB",network_operator: "615d40a21503e000147d4e96",network_category: "M2M",updated_at: "2021-10-06T06:22:26.659Z",__v: 0},{_id: "615d40a21503e000147d4ec0",zone: "C",country: "China",operator_alias: "Idea",network_code: "2551",increment_type: "KB",network_operator: "615d40a21503e000147d4e96",network_category: "M2M",updated_at: "2021-10-06T06:22:26.659Z",__v: 0},{_id: "615d40a21503e000147d4ec1",zone: "C",country: "China",operator_alias: "Airtel",network_code: "2552",increment_type: "KB",network_operator: "615d40a21503e000147d4e96",network_category: "M2M",updated_at: "2021-10-06T06:22:26.660Z",__v: 0},{_id: "615d40a21503e000147d4ec2",zone: "C",country: "China",operator_alias: "Airtel",network_code: "2553",increment_type: "KB",network_operator: "615d40a21503e000147d4e96",network_category: "M2M",updated_at: "2021-10-06T06:22:26.660Z",__v: 0},{_id: "615d40a21503e000147d4ec3",zone: "C",country: "China",operator_alias: "Jio",network_code: "2554",increment_type: "KB",network_operator: "615d40a21503e000147d4e96",network_category: "M2M",updated_at: "2021-10-06T06:22:26.660Z",__v: 0},{_id: "615d40a21503e000147d4ec4",zone: "C",country: "China",operator_alias: "Jio",network_code: "2555",increment_type: "KB",network_operator: "615d40a21503e000147d4e96",network_category: "M2M",updated_at: "2021-10-06T06:22:26.660Z",__v: 0},{_id: "615d40a21503e000147d4ec5",zone: "C",country: "Usa",operator_alias: "Idea",network_code: "2556",increment_type: "KB",network_operator: "615d40a21503e000147d4e96",network_category: "M2M",updated_at: "2021-10-06T06:22:26.660Z",__v: 0},{_id: "615d40a21503e000147d4ec6",zone: "C",country: "Usa",operator_alias: "Idea",network_code: "2557",increment_type: "KB",network_operator: "615d40a21503e000147d4e96",network_category: "M2M",updated_at: "2021-10-06T06:22:26.660Z",__v: 0},{_id: "615d40a21503e000147d4ec7",zone: "C",country: "Usa",operator_alias: "Airtel",network_code: "2558",increment_type: "KB",network_operator: "615d40a21503e000147d4e96",network_category: "M2M",updated_at: "2021-10-06T06:22:26.660Z",__v: 0},{_id: "615d40a21503e000147d4ec8",zone: "C",country: "Usa",operator_alias: "Airtel",network_code: "2559",increment_type: "KB",network_operator: "615d40a21503e000147d4e96",network_category: "M2M",updated_at: "2021-10-06T06:22:26.661Z",__v: 0}];
const zoneMap = new Map();
for (const entry of raw) {
const zone = zoneMap.get(entry.zone) ?? {
zone: entry.zone,
countries: new Map()
};
zoneMap.set(entry.zone, zone);
const country = zone.countries.get(zone.country) ?? {
country: entry.country,
operators: new Map()
};
zone.countries.set(entry.country, country);
const operator = country.operators.get(zone.operator_alias) ?? {
operator: entry.operator_alias
};
country.operators.set(entry.operator_alias, operator);
}
const processed = Array.from(zoneMap.values()).map((o) => ({
zone: o.zone,
countries: Array.from(o.countries.values()).map((c) => ({
country: c.country,
operators: Array.from(c.operators.values())
}))
}));
console.log(processed);
CodePudding user response:
You should be able to get the desired result using Array.reduce()
, we'll use an array of zone objects as the accumulator,
then populate countries, then operators:
let input = [ { "_id": "615d40a21503e000147d4e97", "zone": "A", "country": "India", "operator_alias": "Idea", "network_code": "2590", "increment_type": "KB", "network_operator": "615d40a21503e000147d4e96", "network_category": "M2M", "updated_at": "2021-10-06T06:22:26.653Z", "__v": 0 }, { "_id": "615d40a21503e000147d4e98", "zone": "A", "country": "India", "operator_alias": "Idea", "network_code": "2591", "increment_type": "KB", "network_operator": "615d40a21503e000147d4e96", "network_category": "M2M", "updated_at": "2021-10-06T06:22:26.654Z", "__v": 0 }, { "_id": "615d40a21503e000147d4e99", "zone": "A", "country": "India", "operator_alias": "Idea", "network_code": "2592", "increment_type": "KB", "network_operator": "615d40a21503e000147d4e96", "network_category": "M2M", "updated_at": "2021-10-06T06:22:26.654Z", "__v": 0 }, { "_id": "615d40a21503e000147d4e9a", "zone": "A", "country": "India", "operator_alias": "Idea", "network_code": "2593", "increment_type": "KB", "network_operator": "615d40a21503e000147d4e96", "network_category": "M2M", "updated_at": "2021-10-06T06:22:26.654Z", "__v": 0 }, { "_id": "615d40a21503e000147d4e9b", "zone": "A", "country": "India", "operator_alias": "Airtel", "network_code": "2594", "increment_type": "KB", "network_operator": "615d40a21503e000147d4e96", "network_category": "M2M", "updated_at": "2021-10-06T06:22:26.654Z", "__v": 0 }, { "_id": "615d40a21503e000147d4e9c", "zone": "A", "country": "India", "operator_alias": "Airtel", "network_code": "2595", "increment_type": "KB", "network_operator": "615d40a21503e000147d4e96", "network_category": "M2M", "updated_at": "2021-10-06T06:22:26.654Z", "__v": 0 }, { "_id": "615d40a21503e000147d4e9d", "zone": "A", "country": "India", "operator_alias": "Airtel", "network_code": "2596", "increment_type": "KB", "network_operator": "615d40a21503e000147d4e96", "network_category": "M2M", "updated_at": "2021-10-06T06:22:26.654Z", "__v": 0 }, { "_id": "615d40a21503e000147d4e9e", "zone": "A", "country": "India", "operator_alias": "Jio", "network_code": "2597", "increment_type": "KB", "network_operator": "615d40a21503e000147d4e96", "network_category": "M2M", "updated_at": "2021-10-06T06:22:26.654Z", "__v": 0 }, { "_id": "615d40a21503e000147d4e9f", "zone": "A", "country": "India", "operator_alias": "Jio", "network_code": "2598", "increment_type": "KB", "network_operator": "615d40a21503e000147d4e96", "network_category": "M2M", "updated_at": "2021-10-06T06:22:26.654Z", "__v": 0 }, { "_id": "615d40a21503e000147d4ea0", "zone": "A", "country": "India", "operator_alias": "Jio", "network_code": "2599", "increment_type": "KB", "network_operator": "615d40a21503e000147d4e96", "network_category": "M2M", "updated_at": "2021-10-06T06:22:26.654Z", "__v": 0 }, { "_id": "615d40a21503e000147d4ea1", "zone": "A", "country": "China", "operator_alias": "Idea", "network_code": "2580", "increment_type": "KB", "network_operator": "615d40a21503e000147d4e96", "network_category": "M2M", "updated_at": "2021-10-06T06:22:26.654Z", "__v": 0 }, { "_id": "615d40a21503e000147d4ea2", "zone": "A", "country": "China", "operator_alias": "Idea", "network_code": "2581", "increment_type": "KB", "network_operator": "615d40a21503e000147d4e96", "network_category": "M2M", "updated_at": "2021-10-06T06:22:26.655Z", "__v": 0 }, { "_id": "615d40a21503e000147d4ea3", "zone": "A", "country": "China", "operator_alias": "Airtel", "network_code": "2582", "increment_type": "KB", "network_operator": "615d40a21503e000147d4e96", "network_category": "M2M", "updated_at": "2021-10-06T06:22:26.655Z", "__v": 0 }, { "_id": "615d40a21503e000147d4ea4", "zone": "A", "country": "China", "operator_alias": "Airtel", "network_code": "2583", "increment_type": "KB", "network_operator": "615d40a21503e000147d4e96", "network_category": "M2M", "updated_at": "2021-10-06T06:22:26.655Z", "__v": 0 }, { "_id": "615d40a21503e000147d4ea5", "zone": "A", "country": "China", "operator_alias": "Jio", "network_code": "2584", "increment_type": "KB", "network_operator": "615d40a21503e000147d4e96", "network_category": "M2M", "updated_at": "2021-10-06T06:22:26.655Z", "__v": 0 }, { "_id": "615d40a21503e000147d4ea6", "zone": "A", "country": "China", "operator_alias": "Jio", "network_code": "2585", "increment_type": "KB", "network_operator": "615d40a21503e000147d4e96", "network_category": "M2M", "updated_at": "2021-10-06T06:22:26.655Z", "__v": 0 }, { "_id": "615d40a21503e000147d4ea7", "zone": "A", "country": "France", "operator_alias": "Idea", "network_code": "2586", "increment_type": "KB", "network_operator": "615d40a21503e000147d4e96", "network_category": "M2M", "updated_at": "2021-10-06T06:22:26.655Z", "__v": 0 }, { "_id": "615d40a21503e000147d4ea8", "zone": "A", "country": "France", "operator_alias": "Idea", "network_code": "2587", "increment_type": "KB", "network_operator": "615d40a21503e000147d4e96", "network_category": "M2M", "updated_at": "2021-10-06T06:22:26.656Z", "__v": 0 }, { "_id": "615d40a21503e000147d4ea9", "zone": "A", "country": "France", "operator_alias": "Airtel", "network_code": "2588", "increment_type": "KB", "network_operator": "615d40a21503e000147d4e96", "network_category": "M2M", "updated_at": "2021-10-06T06:22:26.656Z", "__v": 0 }, { "_id": "615d40a21503e000147d4eaa", "zone": "A", "country": "France", "operator_alias": "Airtel", "network_code": "2589", "increment_type": "KB", "network_operator": "615d40a21503e000147d4e96", "network_category": "M2M", "updated_at": "2021-10-06T06:22:26.656Z", "__v": 0 }, { "_id": "615d40a21503e000147d4eab", "zone": "A", "country": "France", "operator_alias": "Jio", "network_code": "2570", "increment_type": "KB", "network_operator": "615d40a21503e000147d4e96", "network_category": "M2M", "updated_at": "2021-10-06T06:22:26.656Z", "__v": 0 }, { "_id": "615d40a21503e000147d4eac", "zone": "A", "country": "France", "operator_alias": "Jio", "network_code": "2571", "increment_type": "KB", "network_operator": "615d40a21503e000147d4e96", "network_category": "M2M", "updated_at": "2021-10-06T06:22:26.657Z", "__v": 0 }, { "_id": "615d40a21503e000147d4ead", "zone": "B", "country": "India", "operator_alias": "Idea", "network_code": "2572", "increment_type": "KB", "network_operator": "615d40a21503e000147d4e96", "network_category": "M2M", "updated_at": "2021-10-06T06:22:26.657Z", "__v": 0 }, { "_id": "615d40a21503e000147d4eae", "zone": "B", "country": "India", "operator_alias": "Idea", "network_code": "2573", "increment_type": "KB", "network_operator": "615d40a21503e000147d4e96", "network_category": "M2M", "updated_at": "2021-10-06T06:22:26.657Z", "__v": 0 }, { "_id": "615d40a21503e000147d4eaf", "zone": "B", "country": "India", "operator_alias": "Idea", "network_code": "2574", "increment_type": "KB", "network_operator": "615d40a21503e000147d4e96", "network_category": "M2M", "updated_at": "2021-10-06T06:22:26.657Z", "__v": 0 }, { "_id": "615d40a21503e000147d4eb0", "zone": "B", "country": "China", "operator_alias": "Airtel", "network_code": "2575", "increment_type": "KB", "network_operator": "615d40a21503e000147d4e96", "network_category": "M2M", "updated_at": "2021-10-06T06:22:26.657Z", "__v": 0 }, { "_id": "615d40a21503e000147d4eb1", "zone": "B", "country": "China", "operator_alias": "Airtel", "network_code": "2576", "increment_type": "KB", "network_operator": "615d40a21503e000147d4e96", "network_category": "M2M", "updated_at": "2021-10-06T06:22:26.657Z", "__v": 0 }, { "_id": "615d40a21503e000147d4eb2", "zone": "B", "country": "China", "operator_alias": "Airtel", "network_code": "2577", "increment_type": "KB", "network_operator": "615d40a21503e000147d4e96", "network_category": "M2M", "updated_at": "2021-10-06T06:22:26.657Z", "__v": 0 }, { "_id": "615d40a21503e000147d4eb3", "zone": "B", "country": "India", "operator_alias": "Airtel", "network_code": "2578", "increment_type": "KB", "network_operator": "615d40a21503e000147d4e96", "network_category": "M2M", "updated_at": "2021-10-06T06:22:26.658Z", "__v": 0 }, { "_id": "615d40a21503e000147d4eb4", "zone": "B", "country": "India", "operator_alias": "Jio", "network_code": "2579", "increment_type": "KB", "network_operator": "615d40a21503e000147d4e96", "network_category": "M2M", "updated_at": "2021-10-06T06:22:26.658Z", "__v": 0 }, { "_id": "615d40a21503e000147d4eb5", "zone": "B", "country": "India", "operator_alias": "Jio", "network_code": "2560", "increment_type": "KB", "network_operator": "615d40a21503e000147d4e96", "network_category": "M2M", "updated_at": "2021-10-06T06:22:26.658Z", "__v": 0 }, { "_id": "615d40a21503e000147d4eb6", "zone": "B", "country": "India", "operator_alias": "Jio", "network_code": "2561", "increment_type": "KB", "network_operator": "615d40a21503e000147d4e96", "network_category": "M2M", "updated_at": "2021-10-06T06:22:26.658Z", "__v": 0 }, { "_id": "615d40a21503e000147d4eb7", "zone": "B", "country": "China", "operator_alias": "Idea", "network_code": "2562", "increment_type": "KB", "network_operator": "615d40a21503e000147d4e96", "network_category": "M2M", "updated_at": "2021-10-06T06:22:26.658Z", "__v": 0 }, { "_id": "615d40a21503e000147d4eb8", "zone": "B", "country": "China", "operator_alias": "Idea", "network_code": "2563", "increment_type": "KB", "network_operator": "615d40a21503e000147d4e96", "network_category": "M2M", "updated_at": "2021-10-06T06:22:26.658Z", "__v": 0 }, { "_id": "615d40a21503e000147d4eb9", "zone": "C", "country": "India", "operator_alias": "Idea", "network_code": "2564", "increment_type": "KB", "network_operator": "615d40a21503e000147d4e96", "network_category": "M2M", "updated_at": "2021-10-06T06:22:26.658Z", "__v": 0 }, { "_id": "615d40a21503e000147d4eba", "zone": "C", "country": "India", "operator_alias": "Idea", "network_code": "2565", "increment_type": "KB", "network_operator": "615d40a21503e000147d4e96", "network_category": "M2M", "updated_at": "2021-10-06T06:22:26.659Z", "__v": 0 }, { "_id": "615d40a21503e000147d4ebb", "zone": "C", "country": "India", "operator_alias": "Idea", "network_code": "2566", "increment_type": "KB", "network_operator": "615d40a21503e000147d4e96", "network_category": "M2M", "updated_at": "2021-10-06T06:22:26.659Z", "__v": 0 }, { "_id": "615d40a21503e000147d4ebc", "zone": "C", "country": "India", "operator_alias": "Airtel", "network_code": "2567", "increment_type": "KB", "network_operator": "615d40a21503e000147d4e96", "network_category": "M2M", "updated_at": "2021-10-06T06:22:26.659Z", "__v": 0 }, { "_id": "615d40a21503e000147d4ebd", "zone": "C", "country": "India", "operator_alias": "Airtel", "network_code": "2568", "increment_type": "KB", "network_operator": "615d40a21503e000147d4e96", "network_category": "M2M", "updated_at": "2021-10-06T06:22:26.659Z", "__v": 0 }, { "_id": "615d40a21503e000147d4ebe", "zone": "C", "country": "India", "operator_alias": "Jio", "network_code": "2569", "increment_type": "KB", "network_operator": "615d40a21503e000147d4e96", "network_category": "M2M", "updated_at": "2021-10-06T06:22:26.659Z", "__v": 0 }, { "_id": "615d40a21503e000147d4ebf", "zone": "C", "country": "China", "operator_alias": "Idea", "network_code": "2550", "increment_type": "KB", "network_operator": "615d40a21503e000147d4e96", "network_category": "M2M", "updated_at": "2021-10-06T06:22:26.659Z", "__v": 0 }, { "_id": "615d40a21503e000147d4ec0", "zone": "C", "country": "China", "operator_alias": "Idea", "network_code": "2551", "increment_type": "KB", "network_operator": "615d40a21503e000147d4e96", "network_category": "M2M", "updated_at": "2021-10-06T06:22:26.659Z", "__v": 0 }, { "_id": "615d40a21503e000147d4ec1", "zone": "C", "country": "China", "operator_alias": "Airtel", "network_code": "2552", "increment_type": "KB", "network_operator": "615d40a21503e000147d4e96", "network_category": "M2M", "updated_at": "2021-10-06T06:22:26.660Z", "__v": 0 }, { "_id": "615d40a21503e000147d4ec2", "zone": "C", "country": "China", "operator_alias": "Airtel", "network_code": "2553", "increment_type": "KB", "network_operator": "615d40a21503e000147d4e96", "network_category": "M2M", "updated_at": "2021-10-06T06:22:26.660Z", "__v": 0 }, { "_id": "615d40a21503e000147d4ec3", "zone": "C", "country": "China", "operator_alias": "Jio", "network_code": "2554", "increment_type": "KB", "network_operator": "615d40a21503e000147d4e96", "network_category": "M2M", "updated_at": "2021-10-06T06:22:26.660Z", "__v": 0 }, { "_id": "615d40a21503e000147d4ec4", "zone": "C", "country": "China", "operator_alias": "Jio", "network_code": "2555", "increment_type": "KB", "network_operator": "615d40a21503e000147d4e96", "network_category": "M2M", "updated_at": "2021-10-06T06:22:26.660Z", "__v": 0 }, { "_id": "615d40a21503e000147d4ec5", "zone": "C", "country": "Usa", "operator_alias": "Idea", "network_code": "2556", "increment_type": "KB", "network_operator": "615d40a21503e000147d4e96", "network_category": "M2M", "updated_at": "2021-10-06T06:22:26.660Z", "__v": 0 }, { "_id": "615d40a21503e000147d4ec6", "zone": "C", "country": "Usa", "operator_alias": "Idea", "network_code": "2557", "increment_type": "KB", "network_operator": "615d40a21503e000147d4e96", "network_category": "M2M", "updated_at": "2021-10-06T06:22:26.660Z", "__v": 0 }, { "_id": "615d40a21503e000147d4ec7", "zone": "C", "country": "Usa", "operator_alias": "Airtel", "network_code": "2558", "increment_type": "KB", "network_operator": "615d40a21503e000147d4e96", "network_category": "M2M", "updated_at": "2021-10-06T06:22:26.660Z", "__v": 0 }, { "_id": "615d40a21503e000147d4ec8", "zone": "C", "country": "Usa", "operator_alias": "Airtel", "network_code": "2559", "increment_type": "KB", "network_operator": "615d40a21503e000147d4e96", "network_category": "M2M", "updated_at": "2021-10-06T06:22:26.661Z", "__v": 0 } ];
const result = input.reduce((acc, { zone, country, operator_alias, network_code }) => {
if (!acc.find(el => el.zone === zone)) {
acc.push( { zone, Zone_Price: '0', Active: true, countries: [] })
}
let zoneObj = acc.find(el => el.zone === zone);
if (!zoneObj.countries.find(el => el.country_name === country)) {
zoneObj.countries.push( { country_name: country, active: true, operators: [] })
}
let countryObj = zoneObj.countries.find(el => el.country_name === country);
if (!countryObj.operators.find(el => el.operator_name === operator_alias)) {
countryObj.operators.push( { operator_name: operator_alias, network_codes: [], active: true })
}
let operatorObj = countryObj.operators.find(el => el.operator_name === operator_alias);
if (!operatorObj.network_codes.find(code => code === network_code)) {
operatorObj.network_codes.push(network_code)
}
return acc;
}, []);
console.log(result)
.as-console-wrapper { max-height: 100% !important; top: 0; }