I want to create a new Array of objects based on Interface Value. Like we need to group objects with the same Interface value. The new array would be like two new Objects with the same interface value. So here in the new generated array of objects, there would be two objects. Such as GE5 and GE6.I have a limitation to use only ES5.
Original Object
[
{
"id": 14042,
"created": "2020-03-18T10:11:40.000Z",
"enterpriseId": 437,
"siteId": 6302,
"activationKey": "PMZP-NGRU-HE64-SA5M",
"activationKeyExpires": "2021-08-04T16:21:33.000Z",
"activationState": "ACTIVATED",
"activationTime": "2020-03-18T17:05:27.000Z",
"softwareVersion": "4.3.0",
"buildNumber": "R430-20210720-GA-64951-67694-6911a00421",
"factorySoftwareVersion": "3.3.0",
"factoryBuildNumber": "R330-MTHD-20190328-GA",
"softwareUpdated": "2021-09-24T11:21:49.000Z",
"selfMacAddress": "50:9a:4c:e4:1e:c0",
"deviceId": "93AB7D33-87B7-42AA-BC0B-7D8255E069AD",
"logicalId": "d25a6121-bf16-4512-b490-2dc5c45e11b8",
"serialNumber": "F0TFXC2",
"modelNumber": "edge610",
"deviceFamily": "EDGE6X0",
"name": "SDALM-BAYNARD-LAB-4",
"dnsName": null,
"description": "Dual Internet Site in Baynard",
"alertsEnabled": 1,
"operatorAlertsEnabled": 1,
"edgeState": "CONNECTED",
"edgeStateTime": "2021-09-24T11:23:00.000Z",
"isLive": 0,
"systemUpSince": "2021-09-24T11:20:55.000Z",
"serviceUpSince": "2021-09-24T11:31:06.000Z",
"lastContact": "2021-10-04T14:06:47.000Z",
"serviceState": "IN_SERVICE",
"endpointPkiMode": "CERTIFICATE_REQUIRED",
"haState": "UNCONFIGURED",
"haPreviousState": "UNCONFIGURED",
"haLastContact": "0000-00-00 00:00:00",
"haSerialNumber": null,
"bastionState": "UNCONFIGURED",
"modified": "2021-10-04T14:06:47.000Z",
"customInfo": "",
"isHub": false,
"cloudServices": [
{
"state": "UP",
"timestamp": "2021-10-04T14:01:50.638Z",
"link": "00000005-bf16-4512-b490-2dc5c45e11b8",
"local_interface_ip": "172.16.2.1",
"local_public_ip": "217.38.39.43",
"nvs_ip": "165.225.16.236",
"pathId": "211262ED9DE488A22CFEBBA9809092B32B937912",
"segmentId": 0,
"segmentLogicalId": "cd948075-e95f-4c7c-beb2-0fcde7e17c62",
"l7_check": "UP",
"l7_check_rtt": {
"max": 11,
"avg": 7,
"min": 5
},
"site": {
"id": 60282,
"logicalId": "717f67c5-7426-4689-a74c-b58c93d4c3b1",
"data": {
"customSourceIp": "",
"linkInternalLogicalId": "00000005-bf16-4512-b490-2dc5c45e11b8",
"primaryAddressing": {
"internalRouterIp": "172.22.60.145",
"internalRouterMask": "255.255.255.255",
"internalZenIp": "172.22.60.146",
"internalZenMask": "255.255.255.255"
},
"secondaryAddressing": {
"internalRouterIp": "172.22.60.149",
"internalRouterMask": "255.255.255.255",
"internalZenIp": "172.22.60.150",
"internalZenMask": "255.255.255.255"
},
"useCustomSourceIp": false
}
},
"provider": {
"name": "L7HC-LON3-MAN1-GRE",
"id": 549697,
"logicalId": "d5a058dc-6202-494c-b65d-6a8c273ed1c4",
"data": {
"primaryServer": "165.225.16.236",
"secondaryServer": "165.225.196.39",
"automateDeployment": false,
"enableTunnels": true,
"sharedIkeAuth": false,
"maxTunnelsPerIkeIdentity": 128,
"l7HealthCheck": {
"enabled": true,
"probeIntervalSec": 5,
"numOfRetries": 3,
"rttThresholdMs": 3000,
"cloud": "zscloud.net"
},
"provider": "zscalerWebSecurityService"
}
},
"segmentName": "Global Segment",
"interface": "GE5"
},
{
"state": "STANDBY",
"timestamp": "2021-10-04T14:01:50.638Z",
"link": "00000005-bf16-4512-b490-2dc5c45e11b8",
"local_interface_ip": "172.16.2.1",
"local_public_ip": "217.38.39.43",
"nvs_ip": "165.225.196.39",
"pathId": "26434D16946CFD147D5DDFA50647F48A0066AB31",
"segmentId": 0,
"segmentLogicalId": "cd948075-e95f-4c7c-beb2-0fcde7e17c62",
"l7_check": "UP",
"l7_check_rtt": {
"max": 10,
"avg": 6,
"min": 5
},
"site": {
"id": 60282,
"logicalId": "717f67c5-7426-4689-a74c-b58c93d4c3b1",
"data": {
"customSourceIp": "",
"linkInternalLogicalId": "00000005-bf16-4512-b490-2dc5c45e11b8",
"primaryAddressing": {
"internalRouterIp": "172.22.60.145",
"internalRouterMask": "255.255.255.255",
"internalZenIp": "172.22.60.146",
"internalZenMask": "255.255.255.255"
},
"secondaryAddressing": {
"internalRouterIp": "172.22.60.149",
"internalRouterMask": "255.255.255.255",
"internalZenIp": "172.22.60.150",
"internalZenMask": "255.255.255.255"
},
"useCustomSourceIp": false
}
},
"provider": {
"name": "L7HC-LON3-MAN1-GRE",
"id": 549697,
"logicalId": "d5a058dc-6202-494c-b65d-6a8c273ed1c4",
"data": {
"primaryServer": "165.225.16.236",
"secondaryServer": "165.225.196.39",
"automateDeployment": false,
"enableTunnels": true,
"sharedIkeAuth": false,
"maxTunnelsPerIkeIdentity": 128,
"l7HealthCheck": {
"enabled": true,
"probeIntervalSec": 5,
"numOfRetries": 3,
"rttThresholdMs": 3000,
"cloud": "zscloud.net"
},
"provider": "zscalerWebSecurityService"
}
},
"segmentName": "Global Segment",
"interface": "GE5"
},
{
"state": "STANDBY",
"timestamp": "2021-10-04T14:01:50.638Z",
"link": "00000006-bf16-4512-b490-2dc5c45e11b8",
"local_interface_ip": "172.16.1.1",
"local_public_ip": "217.38.39.41",
"nvs_ip": "165.225.196.39",
"pathId": "9B88047C891952D8A661F30A8E7C0A5842AB8544",
"segmentId": 0,
"segmentLogicalId": "cd948075-e95f-4c7c-beb2-0fcde7e17c62",
"l7_check": "UP",
"l7_check_rtt": {
"max": 16,
"avg": 6,
"min": 5
},
"site": {
"id": 60282,
"logicalId": "717f67c5-7426-4689-a74c-b58c93d4c3b1",
"data": {
"customSourceIp": "",
"linkInternalLogicalId": "00000005-bf16-4512-b490-2dc5c45e11b8",
"primaryAddressing": {
"internalRouterIp": "172.22.60.145",
"internalRouterMask": "255.255.255.255",
"internalZenIp": "172.22.60.146",
"internalZenMask": "255.255.255.255"
},
"secondaryAddressing": {
"internalRouterIp": "172.22.60.149",
"internalRouterMask": "255.255.255.255",
"internalZenIp": "172.22.60.150",
"internalZenMask": "255.255.255.255"
},
"useCustomSourceIp": false
}
},
"provider": {
"name": "L7HC-LON3-MAN1-GRE",
"id": 549697,
"logicalId": "d5a058dc-6202-494c-b65d-6a8c273ed1c4",
"data": {
"primaryServer": "165.225.16.236",
"secondaryServer": "165.225.196.39",
"automateDeployment": false,
"enableTunnels": true,
"sharedIkeAuth": false,
"maxTunnelsPerIkeIdentity": 128,
"l7HealthCheck": {
"enabled": true,
"probeIntervalSec": 5,
"numOfRetries": 3,
"rttThresholdMs": 3000,
"cloud": "zscloud.net"
},
"provider": "zscalerWebSecurityService"
}
},
"segmentName": "Global Segment",
"interface": "GE6"
},
{
"state": "UP",
"timestamp": "2021-10-04T14:01:50.638Z",
"link": "00000006-bf16-4512-b490-2dc5c45e11b8",
"local_interface_ip": "172.16.1.1",
"local_public_ip": "217.38.39.41",
"nvs_ip": "165.225.16.236",
"pathId": "DB965CA91A564DE09027F8D766F92CD0DDB54405",
"segmentId": 0,
"segmentLogicalId": "cd948075-e95f-4c7c-beb2-0fcde7e17c62",
"l7_check": "UP",
"l7_check_rtt": {
"max": 1063,
"avg": 41,
"min": 5
},
"site": {
"id": 60282,
"logicalId": "717f67c5-7426-4689-a74c-b58c93d4c3b1",
"data": {
"customSourceIp": "",
"linkInternalLogicalId": "00000005-bf16-4512-b490-2dc5c45e11b8",
"primaryAddressing": {
"internalRouterIp": "172.22.60.145",
"internalRouterMask": "255.255.255.255",
"internalZenIp": "172.22.60.146",
"internalZenMask": "255.255.255.255"
},
"secondaryAddressing": {
"internalRouterIp": "172.22.60.149",
"internalRouterMask": "255.255.255.255",
"internalZenIp": "172.22.60.150",
"internalZenMask": "255.255.255.255"
},
"useCustomSourceIp": false
}
},
"provider": {
"name": "L7HC-LON3-MAN1-GRE",
"id": 549697,
"logicalId": "d5a058dc-6202-494c-b65d-6a8c273ed1c4",
"data": {
"primaryServer": "165.225.16.236",
"secondaryServer": "165.225.196.39",
"automateDeployment": false,
"enableTunnels": true,
"sharedIkeAuth": false,
"maxTunnelsPerIkeIdentity": 128,
"l7HealthCheck": {
"enabled": true,
"probeIntervalSec": 5,
"numOfRetries": 3,
"rttThresholdMs": 3000,
"cloud": "zscloud.net"
},
"provider": "zscalerWebSecurityService"
}
},
"segmentName": "Global Segment",
"interface": "GE6"
}
]
},
]
CodePudding user response:
There should be a looping logic
Logic
- Loop through the
data
array. Which is the input. - Loop through
cloudServices
nodes in each node of thedata
and find the distict value. - Loop through this distinct values and and generate a group of each distict value using
Array.filter
. - Aggregate all the remainig values from each node in data array except the
cloudServices
node. - Generate a final object with this remaining values and the above generated group.
ES5 implementation
var data = [{ "id": 14042, "created": "2020-03-18T10:11:40.000Z", "enterpriseId": 437, "siteId": 6302, "activationKey": "PMZP-NGRU-HE64-SA5M", "activationKeyExpires": "2021-08-04T16:21:33.000Z", "activationState": "ACTIVATED", "activationTime": "2020-03-18T17:05:27.000Z", "softwareVersion": "4.3.0", "buildNumber": "R430-20210720-GA-64951-67694-6911a00421", "factorySoftwareVersion": "3.3.0", "factoryBuildNumber": "R330-MTHD-20190328-GA", "softwareUpdated": "2021-09-24T11:21:49.000Z", "selfMacAddress": "50:9a:4c:e4:1e:c0", "deviceId": "93AB7D33-87B7-42AA-BC0B-7D8255E069AD", "logicalId": "d25a6121-bf16-4512-b490-2dc5c45e11b8", "serialNumber": "F0TFXC2", "modelNumber": "edge610", "deviceFamily": "EDGE6X0", "name": "SDALM-BAYNARD-LAB-4", "dnsName": null, "description": "Dual Internet Site in Baynard", "alertsEnabled": 1, "operatorAlertsEnabled": 1, "edgeState": "CONNECTED", "edgeStateTime": "2021-09-24T11:23:00.000Z", "isLive": 0, "systemUpSince": "2021-09-24T11:20:55.000Z", "serviceUpSince": "2021-09-24T11:31:06.000Z", "lastContact": "2021-10-04T14:06:47.000Z", "serviceState": "IN_SERVICE", "endpointPkiMode": "CERTIFICATE_REQUIRED", "haState": "UNCONFIGURED", "haPreviousState": "UNCONFIGURED", "haLastContact": "0000-00-00 00:00:00", "haSerialNumber": null, "bastionState": "UNCONFIGURED", "modified": "2021-10-04T14:06:47.000Z", "customInfo": "", "isHub": false, "cloudServices": [{ "state": "UP", "timestamp": "2021-10-04T14:01:50.638Z", "link": "00000005-bf16-4512-b490-2dc5c45e11b8", "local_interface_ip": "172.16.2.1", "local_public_ip": "217.38.39.43", "nvs_ip": "165.225.16.236", "pathId": "211262ED9DE488A22CFEBBA9809092B32B937912", "segmentId": 0, "segmentLogicalId": "cd948075-e95f-4c7c-beb2-0fcde7e17c62", "l7_check": "UP", "l7_check_rtt": { "max": 11, "avg": 7, "min": 5 }, "site": { "id": 60282, "logicalId": "717f67c5-7426-4689-a74c-b58c93d4c3b1", "data": { "customSourceIp": "", "linkInternalLogicalId": "00000005-bf16-4512-b490-2dc5c45e11b8", "primaryAddressing": { "internalRouterIp": "172.22.60.145", "internalRouterMask": "255.255.255.255", "internalZenIp": "172.22.60.146", "internalZenMask": "255.255.255.255" }, "secondaryAddressing": { "internalRouterIp": "172.22.60.149", "internalRouterMask": "255.255.255.255", "internalZenIp": "172.22.60.150", "internalZenMask": "255.255.255.255" }, "useCustomSourceIp": false } }, "provider": { "name": "L7HC-LON3-MAN1-GRE", "id": 549697, "logicalId": "d5a058dc-6202-494c-b65d-6a8c273ed1c4", "data": { "primaryServer": "165.225.16.236", "secondaryServer": "165.225.196.39", "automateDeployment": false, "enableTunnels": true, "sharedIkeAuth": false, "maxTunnelsPerIkeIdentity": 128, "l7HealthCheck": { "enabled": true, "probeIntervalSec": 5, "numOfRetries": 3, "rttThresholdMs": 3000, "cloud": "zscloud.net" }, "provider": "zscalerWebSecurityService" } }, "segmentName": "Global Segment", "interface": "GE5" }, { "state": "STANDBY", "timestamp": "2021-10-04T14:01:50.638Z", "link": "00000005-bf16-4512-b490-2dc5c45e11b8", "local_interface_ip": "172.16.2.1", "local_public_ip": "217.38.39.43", "nvs_ip": "165.225.196.39", "pathId": "26434D16946CFD147D5DDFA50647F48A0066AB31", "segmentId": 0, "segmentLogicalId": "cd948075-e95f-4c7c-beb2-0fcde7e17c62", "l7_check": "UP", "l7_check_rtt": { "max": 10, "avg": 6, "min": 5 }, "site": { "id": 60282, "logicalId": "717f67c5-7426-4689-a74c-b58c93d4c3b1", "data": { "customSourceIp": "", "linkInternalLogicalId": "00000005-bf16-4512-b490-2dc5c45e11b8", "primaryAddressing": { "internalRouterIp": "172.22.60.145", "internalRouterMask": "255.255.255.255", "internalZenIp": "172.22.60.146", "internalZenMask": "255.255.255.255" }, "secondaryAddressing": { "internalRouterIp": "172.22.60.149", "internalRouterMask": "255.255.255.255", "internalZenIp": "172.22.60.150", "internalZenMask": "255.255.255.255" }, "useCustomSourceIp": false } }, "provider": { "name": "L7HC-LON3-MAN1-GRE", "id": 549697, "logicalId": "d5a058dc-6202-494c-b65d-6a8c273ed1c4", "data": { "primaryServer": "165.225.16.236", "secondaryServer": "165.225.196.39", "automateDeployment": false, "enableTunnels": true, "sharedIkeAuth": false, "maxTunnelsPerIkeIdentity": 128, "l7HealthCheck": { "enabled": true, "probeIntervalSec": 5, "numOfRetries": 3, "rttThresholdMs": 3000, "cloud": "zscloud.net" }, "provider": "zscalerWebSecurityService" } }, "segmentName": "Global Segment", "interface": "GE5" }, { "state": "STANDBY", "timestamp": "2021-10-04T14:01:50.638Z", "link": "00000006-bf16-4512-b490-2dc5c45e11b8", "local_interface_ip": "172.16.1.1", "local_public_ip": "217.38.39.41", "nvs_ip": "165.225.196.39", "pathId": "9B88047C891952D8A661F30A8E7C0A5842AB8544", "segmentId": 0, "segmentLogicalId": "cd948075-e95f-4c7c-beb2-0fcde7e17c62", "l7_check": "UP", "l7_check_rtt": { "max": 16, "avg": 6, "min": 5 }, "site": { "id": 60282, "logicalId": "717f67c5-7426-4689-a74c-b58c93d4c3b1", "data": { "customSourceIp": "", "linkInternalLogicalId": "00000005-bf16-4512-b490-2dc5c45e11b8", "primaryAddressing": { "internalRouterIp": "172.22.60.145", "internalRouterMask": "255.255.255.255", "internalZenIp": "172.22.60.146", "internalZenMask": "255.255.255.255" }, "secondaryAddressing": { "internalRouterIp": "172.22.60.149", "internalRouterMask": "255.255.255.255", "internalZenIp": "172.22.60.150", "internalZenMask": "255.255.255.255" }, "useCustomSourceIp": false } }, "provider": { "name": "L7HC-LON3-MAN1-GRE", "id": 549697, "logicalId": "d5a058dc-6202-494c-b65d-6a8c273ed1c4", "data": { "primaryServer": "165.225.16.236", "secondaryServer": "165.225.196.39", "automateDeployment": false, "enableTunnels": true, "sharedIkeAuth": false, "maxTunnelsPerIkeIdentity": 128, "l7HealthCheck": { "enabled": true, "probeIntervalSec": 5, "numOfRetries": 3, "rttThresholdMs": 3000, "cloud": "zscloud.net" }, "provider": "zscalerWebSecurityService" } }, "segmentName": "Global Segment", "interface": "GE6" }, { "state": "UP", "timestamp": "2021-10-04T14:01:50.638Z", "link": "00000006-bf16-4512-b490-2dc5c45e11b8", "local_interface_ip": "172.16.1.1", "local_public_ip": "217.38.39.41", "nvs_ip": "165.225.16.236", "pathId": "DB965CA91A564DE09027F8D766F92CD0DDB54405", "segmentId": 0, "segmentLogicalId": "cd948075-e95f-4c7c-beb2-0fcde7e17c62", "l7_check": "UP", "l7_check_rtt": { "max": 1063, "avg": 41, "min": 5 }, "site": { "id": 60282, "logicalId": "717f67c5-7426-4689-a74c-b58c93d4c3b1", "data": { "customSourceIp": "", "linkInternalLogicalId": "00000005-bf16-4512-b490-2dc5c45e11b8", "primaryAddressing": { "internalRouterIp": "172.22.60.145", "internalRouterMask": "255.255.255.255", "internalZenIp": "172.22.60.146", "internalZenMask": "255.255.255.255" }, "secondaryAddressing": { "internalRouterIp": "172.22.60.149", "internalRouterMask": "255.255.255.255", "internalZenIp": "172.22.60.150", "internalZenMask": "255.255.255.255" }, "useCustomSourceIp": false } }, "provider": { "name": "L7HC-LON3-MAN1-GRE", "id": 549697, "logicalId": "d5a058dc-6202-494c-b65d-6a8c273ed1c4", "data": { "primaryServer": "165.225.16.236", "secondaryServer": "165.225.196.39", "automateDeployment": false, "enableTunnels": true, "sharedIkeAuth": false, "maxTunnelsPerIkeIdentity": 128, "l7HealthCheck": { "enabled": true, "probeIntervalSec": 5, "numOfRetries": 3, "rttThresholdMs": 3000, "cloud": "zscloud.net" }, "provider": "zscalerWebSecurityService" } }, "segmentName": "Global Segment", "interface": "GE6" }] }];
//Function to find distinct valies of an object array with specific key
function distinct(dataArray, key) {
var distinctList = [];
dataArray.forEach(function (node) {
if (distinctList.indexOf(node[key]) === -1) {
distinctList.push(node[key]);
}
});
return distinctList;
}
// Reference https://stackoverflow.com/a/34710102/6099327
function _objectWithoutProperties(obj, keys) {
var target = {};
for (var i in obj) {
if (keys.indexOf(i) >= 0) {
continue;
}
if (!Object.prototype.hasOwnProperty.call(obj, i)) {
continue;
}
target[i] = obj[i];
}
return target;
}
var parsedOutput = [];
data.forEach(function (dataNode) {
var distinctInterfaces = distinct(dataNode.cloudServices, "interface");
var remainingProperties = _objectWithoutProperties(dataNode, ["cloudServices"]);
var output = remainingProperties;
distinctInterfaces.forEach(function (interface) {
output[interface] = dataNode.cloudServices.filter(function (node) {
return node.interface === interface;
});
});
parsedOutput.push(output);
});
console.log(parsedOutput);
ES6 Implementation will be a little more easy
const data = [{"id":14042,"created":"2020-03-18T10:11:40.000Z","enterpriseId":437,"siteId":6302,"activationKey":"PMZP-NGRU-HE64-SA5M","activationKeyExpires":"2021-08-04T16:21:33.000Z","activationState":"ACTIVATED","activationTime":"2020-03-18T17:05:27.000Z","softwareVersion":"4.3.0","buildNumber":"R430-20210720-GA-64951-67694-6911a00421","factorySoftwareVersion":"3.3.0","factoryBuildNumber":"R330-MTHD-20190328-GA","softwareUpdated":"2021-09-24T11:21:49.000Z","selfMacAddress":"50:9a:4c:e4:1e:c0","deviceId":"93AB7D33-87B7-42AA-BC0B-7D8255E069AD","logicalId":"d25a6121-bf16-4512-b490-2dc5c45e11b8","serialNumber":"F0TFXC2","modelNumber":"edge610","deviceFamily":"EDGE6X0","name":"SDALM-BAYNARD-LAB-4","dnsName":null,"description":"Dual Internet Site in Baynard","alertsEnabled":1,"operatorAlertsEnabled":1,"edgeState":"CONNECTED","edgeStateTime":"2021-09-24T11:23:00.000Z","isLive":0,"systemUpSince":"2021-09-24T11:20:55.000Z","serviceUpSince":"2021-09-24T11:31:06.000Z","lastContact":"2021-10-04T14:06:47.000Z","serviceState":"IN_SERVICE","endpointPkiMode":"CERTIFICATE_REQUIRED","haState":"UNCONFIGURED","haPreviousState":"UNCONFIGURED","haLastContact":"0000-00-00 00:00:00","haSerialNumber":null,"bastionState":"UNCONFIGURED","modified":"2021-10-04T14:06:47.000Z","customInfo":"","isHub":false,"cloudServices":[{"state":"UP","timestamp":"2021-10-04T14:01:50.638Z","link":"00000005-bf16-4512-b490-2dc5c45e11b8","local_interface_ip":"172.16.2.1","local_public_ip":"217.38.39.43","nvs_ip":"165.225.16.236","pathId":"211262ED9DE488A22CFEBBA9809092B32B937912","segmentId":0,"segmentLogicalId":"cd948075-e95f-4c7c-beb2-0fcde7e17c62","l7_check":"UP","l7_check_rtt":{"max":11,"avg":7,"min":5},"site":{"id":60282,"logicalId":"717f67c5-7426-4689-a74c-b58c93d4c3b1","data":{"customSourceIp":"","linkInternalLogicalId":"00000005-bf16-4512-b490-2dc5c45e11b8","primaryAddressing":{"internalRouterIp":"172.22.60.145","internalRouterMask":"255.255.255.255","internalZenIp":"172.22.60.146","internalZenMask":"255.255.255.255"},"secondaryAddressing":{"internalRouterIp":"172.22.60.149","internalRouterMask":"255.255.255.255","internalZenIp":"172.22.60.150","internalZenMask":"255.255.255.255"},"useCustomSourceIp":false}},"provider":{"name":"L7HC-LON3-MAN1-GRE","id":549697,"logicalId":"d5a058dc-6202-494c-b65d-6a8c273ed1c4","data":{"primaryServer":"165.225.16.236","secondaryServer":"165.225.196.39","automateDeployment":false,"enableTunnels":true,"sharedIkeAuth":false,"maxTunnelsPerIkeIdentity":128,"l7HealthCheck":{"enabled":true,"probeIntervalSec":5,"numOfRetries":3,"rttThresholdMs":3000,"cloud":"zscloud.net"},"provider":"zscalerWebSecurityService"}},"segmentName":"Global Segment","interface":"GE5"},{"state":"STANDBY","timestamp":"2021-10-04T14:01:50.638Z","link":"00000005-bf16-4512-b490-2dc5c45e11b8","local_interface_ip":"172.16.2.1","local_public_ip":"217.38.39.43","nvs_ip":"165.225.196.39","pathId":"26434D16946CFD147D5DDFA50647F48A0066AB31","segmentId":0,"segmentLogicalId":"cd948075-e95f-4c7c-beb2-0fcde7e17c62","l7_check":"UP","l7_check_rtt":{"max":10,"avg":6,"min":5},"site":{"id":60282,"logicalId":"717f67c5-7426-4689-a74c-b58c93d4c3b1","data":{"customSourceIp":"","linkInternalLogicalId":"00000005-bf16-4512-b490-2dc5c45e11b8","primaryAddressing":{"internalRouterIp":"172.22.60.145","internalRouterMask":"255.255.255.255","internalZenIp":"172.22.60.146","internalZenMask":"255.255.255.255"},"secondaryAddressing":{"internalRouterIp":"172.22.60.149","internalRouterMask":"255.255.255.255","internalZenIp":"172.22.60.150","internalZenMask":"255.255.255.255"},"useCustomSourceIp":false}},"provider":{"name":"L7HC-LON3-MAN1-GRE","id":549697,"logicalId":"d5a058dc-6202-494c-b65d-6a8c273ed1c4","data":{"primaryServer":"165.225.16.236","secondaryServer":"165.225.196.39","automateDeployment":false,"enableTunnels":true,"sharedIkeAuth":false,"maxTunnelsPerIkeIdentity":128,"l7HealthCheck":{"enabled":true,"probeIntervalSec":5,"numOfRetries":3,"rttThresholdMs":3000,"cloud":"zscloud.net"},"provider":"zscalerWebSecurityService"}},"segmentName":"Global Segment","interface":"GE5"},{"state":"STANDBY","timestamp":"2021-10-04T14:01:50.638Z","link":"00000006-bf16-4512-b490-2dc5c45e11b8","local_interface_ip":"172.16.1.1","local_public_ip":"217.38.39.41","nvs_ip":"165.225.196.39","pathId":"9B88047C891952D8A661F30A8E7C0A5842AB8544","segmentId":0,"segmentLogicalId":"cd948075-e95f-4c7c-beb2-0fcde7e17c62","l7_check":"UP","l7_check_rtt":{"max":16,"avg":6,"min":5},"site":{"id":60282,"logicalId":"717f67c5-7426-4689-a74c-b58c93d4c3b1","data":{"customSourceIp":"","linkInternalLogicalId":"00000005-bf16-4512-b490-2dc5c45e11b8","primaryAddressing":{"internalRouterIp":"172.22.60.145","internalRouterMask":"255.255.255.255","internalZenIp":"172.22.60.146","internalZenMask":"255.255.255.255"},"secondaryAddressing":{"internalRouterIp":"172.22.60.149","internalRouterMask":"255.255.255.255","internalZenIp":"172.22.60.150","internalZenMask":"255.255.255.255"},"useCustomSourceIp":false}},"provider":{"name":"L7HC-LON3-MAN1-GRE","id":549697,"logicalId":"d5a058dc-6202-494c-b65d-6a8c273ed1c4","data":{"primaryServer":"165.225.16.236","secondaryServer":"165.225.196.39","automateDeployment":false,"enableTunnels":true,"sharedIkeAuth":false,"maxTunnelsPerIkeIdentity":128,"l7HealthCheck":{"enabled":true,"probeIntervalSec":5,"numOfRetries":3,"rttThresholdMs":3000,"cloud":"zscloud.net"},"provider":"zscalerWebSecurityService"}},"segmentName":"Global Segment","interface":"GE6"},{"state":"UP","timestamp":"2021-10-04T14:01:50.638Z","link":"00000006-bf16-4512-b490-2dc5c45e11b8","local_interface_ip":"172.16.1.1","local_public_ip":"217.38.39.41","nvs_ip":"165.225.16.236","pathId":"DB965CA91A564DE09027F8D766F92CD0DDB54405","segmentId":0,"segmentLogicalId":"cd948075-e95f-4c7c-beb2-0fcde7e17c62","l7_check":"UP","l7_check_rtt":{"max":1063,"avg":41,"min":5},"site":{"id":60282,"logicalId":"717f67c5-7426-4689-a74c-b58c93d4c3b1","data":{"customSourceIp":"","linkInternalLogicalId":"00000005-bf16-4512-b490-2dc5c45e11b8","primaryAddressing":{"internalRouterIp":"172.22.60.145","internalRouterMask":"255.255.255.255","internalZenIp":"172.22.60.146","internalZenMask":"255.255.255.255"},"secondaryAddressing":{"internalRouterIp":"172.22.60.149","internalRouterMask":"255.255.255.255","internalZenIp":"172.22.60.150","internalZenMask":"255.255.255.255"},"useCustomSourceIp":false}},"provider":{"name":"L7HC-LON3-MAN1-GRE","id":549697,"logicalId":"d5a058dc-6202-494c-b65d-6a8c273ed1c4","data":{"primaryServer":"165.225.16.236","secondaryServer":"165.225.196.39","automateDeployment":false,"enableTunnels":true,"sharedIkeAuth":false,"maxTunnelsPerIkeIdentity":128,"l7HealthCheck":{"enabled":true,"probeIntervalSec":5,"numOfRetries":3,"rttThresholdMs":3000,"cloud":"zscloud.net"},"provider":"zscalerWebSecurityService"}},"segmentName":"Global Segment","interface":"GE6"}]}];
const parsedOutput = data.map((node) => {
const { cloudServices, ...otherNodes } = node;
const uniqueInterfaces = [ ...new Set(cloudServices.map(service => service.interface)) ];
interfaceObj = uniqueInterfaces.reduce((acc, curr) => {
acc[curr] = cloudServices.filter((node) => node.interface === curr);
return acc;
}, {})
return { ...otherNodes, ...interfaceObj };
});
console.log(parsedOutput)
CodePudding user response:
An easy way to create-
interface simpleInt {
id: number;
label: string;
key: any;
}
type simpleType = simpleInt[];