Create new Array of object based on the Property Value - javascript
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.
There should be a looping logic
Logic
Loop through the data array. Which is the input.
Loop through cloudServices nodes in each node of the data 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)
Related
Find a key value based on a match in Javascript
I'm trying to return the uuid where the urns key has this match. "urns": [ "whatsapp:27820111111" ], What should be returned d7b9f88c-b359-4d69-926d-536c0d2892e0 since it has that phone number match. How can this be done programmatically. Here's are the two dicts. { "next": "https://random.com=", "previous": null, "results": [ { "uuid": "g2ws8dh5-b359-4d69-926d-536c0d2892e0", "name": "James", "language": "eng", "urns": [ "whatsapp:27333333333" ], "groups": [ { "uuid": "57fs8430-3c78-42b7-876c-c385e36c1dba", "name": "Postbirth WhatsApp" } ], "fields": { "whatsapp_consent": null, "webhook_failure_count": null, }, "blocked": false, "stopped": false, "created_on": "2020-12-01T07:50:58.736502Z", "modified_on": "2020-12-01T08:36:34.980359Z" }, { "uuid": "d7b9f88c-b359-4d69-926d-536c0d2892e0", "name": "Jane", "language": "eng", "urns": [ "whatsapp:27820111111" ], "groups": [ { "uuid": "e35f2e39-3c78-42b7-876c-c385e36c1dba", "name": "Prebirth WhatsApp" } ], "fields": { "whatsapp_consent": null, "webhook_failure_count": null, }, "blocked": false, "stopped": false, "created_on": "2020-12-01T07:50:58.736502Z", "modified_on": "2020-12-01T08:36:34.980359Z" } ] } I'm able to loop through and find the urn but I'm not sure how to step back to get the uuid.
Pass in the results array and the urn string to this function. If found it will return the uuid, else it will return null. function getIdFromUrn(results, urn) { for (let i = 0; i < results.length; i++) { const result = results[i]; if (result.urns.includes(urn)) { return result.uuid; // urn found } } return null; // not found } console.log(getIdFromUrn(results, 'whatsapp:27820111111'));
You don't need to loop when you can use find(...) to match against a value. See demo below var data = [{ "uuid": "g2ws8dh5-b359-4d69-926d-536c0d2892e0", "name": "James", "language": "eng", "urns": [ "whatsapp:27333333333" ], "groups": [{ "uuid": "57fs8430-3c78-42b7-876c-c385e36c1dba", "name": "Postbirth WhatsApp" }], "fields": { "whatsapp_consent": null, "webhook_failure_count": null, }, "blocked": false, "stopped": false, "created_on": "2020-12-01T07:50:58.736502Z", "modified_on": "2020-12-01T08:36:34.980359Z" }, { "uuid": "d7b9f88c-b359-4d69-926d-536c0d2892e0", "name": "Jane", "language": "eng", "urns": [ "whatsapp:27820111111" ], "groups": [{ "uuid": "e35f2e39-3c78-42b7-876c-c385e36c1dba", "name": "Prebirth WhatsApp" }], "fields": { "whatsapp_consent": null, "webhook_failure_count": null, }, "blocked": false, "stopped": false, "created_on": "2020-12-01T07:50:58.736502Z", "modified_on": "2020-12-01T08:36:34.980359Z" } ]; const lookingFor = "whatsapp:27820111111"; const foundIt = data.find(el => { return (el.urns.indexOf(lookingFor) !== -1); }); console.log(foundIt.uuid);
Unknown regularizer: L2 in tensorflowjs
I have trained a model in python using model reg = 0.000001 model = Sequential() model.add(Dense(24, activation='tanh', name='input_dense', input_shape=input_shape)) model.add(GRU(24, activation='tanh', recurrent_activation='sigmoid', return_sequences=True, kernel_regularizer=regularizers.l2(reg), recurrent_regularizer=regularizers.l2(reg), reset_after=False)) model.add(Flatten()) model.add(Dense(2, activation='softmax')) But when I converted this model using "tensorflowjs_converter --input_format keras" and loaded in browser getting error Unhandled Rejection (Error): Unknown regularizer: L2. This may be due to one of the following reasons: The regularizer is defined in Python, in which case it needs to be ported to TensorFlow.js or your JavaScript code. The custom regularizer is defined in JavaScript, but is not registered properly with tf.serialization.registerClass(). The model.json file content is { "format": "layers-model", "generatedBy": "keras v2.4.0", "convertedBy": "TensorFlow.js Converter v2.3.0", "modelTopology": { "keras_version": "2.4.0", "backend": "tensorflow", "model_config": { "class_name": "Sequential", "config": { "name": "sequential", "layers": [ { "class_name": "InputLayer", "config": { "batch_input_shape": [null, 22, 13], "dtype": "float32", "sparse": false, "ragged": false, "name": "input_dense_input" } }, { "class_name": "Dense", "config": { "name": "input_dense", "trainable": true, "batch_input_shape": [null, 22, 13], "dtype": "float32", "units": 24, "activation": "tanh", "use_bias": true, "kernel_initializer": { "class_name": "GlorotUniform", "config": { "seed": null } }, "bias_initializer": { "class_name": "Zeros", "config": {} }, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null } }, { "class_name": "GRU", "config": { "name": "gru", "trainable": true, "dtype": "float32", "return_sequences": true, "return_state": false, "go_backwards": false, "stateful": false, "unroll": false, "time_major": false, "units": 24, "activation": "tanh", "recurrent_activation": "sigmoid", "use_bias": true, "kernel_initializer": { "class_name": "GlorotUniform", "config": { "seed": null } }, "recurrent_initializer": { "class_name": "Orthogonal", "config": { "gain": 1.0, "seed": null } }, "bias_initializer": { "class_name": "Zeros", "config": {} }, "kernel_regularizer": { "class_name": "L2", "config": { "l2": 9.999999974752427e-7 } }, "recurrent_regularizer": { "class_name": "L2", "config": { "l2": 9.999999974752427e-7 } }, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "recurrent_constraint": null, "bias_constraint": null, "dropout": 0.0, "recurrent_dropout": 0.0, "implementation": 2, "reset_after": false } }, { "class_name": "Flatten", "config": { "name": "flatten", "trainable": true, "dtype": "float32", "data_format": "channels_last" } }, { "class_name": "Dense", "config": { "name": "dense", "trainable": true, "dtype": "float32", "units": 2, "activation": "softmax", "use_bias": true, "kernel_initializer": { "class_name": "GlorotUniform", "config": { "seed": null } }, "bias_initializer": { "class_name": "Zeros", "config": {} }, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null } } ] } }, "training_config": { "loss": "categorical_crossentropy", "metrics": ["accuracy"], "weighted_metrics": null, "loss_weights": null, "optimizer_config": { "class_name": "Nadam", "config": { "name": "Nadam", "learning_rate": 0.0020000000949949026, "decay": 0.004000000189989805, "beta_1": 0.8999999761581421, "beta_2": 0.9990000128746033, "epsilon": 1e-7 } } } }, "weightsManifest": [ { "paths": ["group1-shard1of1.bin"], "weights": [ { "name": "dense/kernel", "shape": [528, 2], "dtype": "float32" }, { "name": "dense/bias", "shape": [2], "dtype": "float32" }, { "name": "gru/gru_cell/kernel", "shape": [24, 72], "dtype": "float32" }, { "name": "gru/gru_cell/recurrent_kernel", "shape": [24, 72], "dtype": "float32" }, { "name": "gru/gru_cell/bias", "shape": [72], "dtype": "float32" }, { "name": "input_dense/kernel", "shape": [13, 24], "dtype": "float32" }, { "name": "input_dense/bias", "shape": [24], "dtype": "float32" } ] } ] }
Option 1 There are no classes L1 and L2; they are just interfaces(more here) There is a class L1L2 which will take the config and return the right regularizer. You can manually replace all occurences of L2 to L1L2. Option 2 Register a class L2 class L2 { static className = 'L2'; constructor(config) { return tf.regularizers.l1l2(config) } } tf.serialization.registerClass(L2); // now load the model
Flatten an object using lodash
I have below this nested object I need to create an array using this object containing keys. And if keys are object then it should use .dot syntax. and if it is an array then it should give me key.0.keyName. Is it possible to do so? Output [ "AllowIPNPayment", "AllowOnlinePayment", "MetaData.CreateTime", "MetaData.LastUpdatedTime", "CustomField.0.DefinitionId", "CustomField.0.Name", "CustomField.0.Type", ... ] What I have tried is just ugly and does give me expected result. If it is possible with more concise way. const invoiceObject = { "AllowIPNPayment": false, "AllowOnlinePayment": false, "AllowOnlineCreditCardPayment": false, "AllowOnlineACHPayment": false, "domain": "QBO", "sparse": false, "Id": "16", "SyncToken": "1", "MetaData": { "CreateTime": "2020-03-25T15:10:40-07:00", "LastUpdatedTime": "2020-03-26T11:06:49-07:00" }, "CustomField": [{ "DefinitionId": "1", "Name": "Crew #", "Type": "StringType" }], "DocNumber": "1007", "TxnDate": "2020-03-03", "CurrencyRef": { "value": "USD", "name": "United States Dollar" }, "LinkedTxn": [{ "TxnId": "32", "TxnType": "Payment" }], "Line": [{ "Id": "1", "LineNum": 1, "Description": "Custom Design", "Amount": 750, "DetailType": "SalesItemLineDetail", "SalesItemLineDetail": { "ItemRef": { "value": "4", "name": "Design" }, "UnitPrice": 75, "Qty": 10, "TaxCodeRef": { "value": "NON" } } }, { "Amount": 750, "DetailType": "SubTotalLineDetail", "SubTotalLineDetail": {} } ], "TxnTaxDetail": { "TotalTax": 0 }, "CustomerRef": { "value": "13", "name": "uiool" }, "CustomerMemo": { "value": "Thank you for your business and have a great day!" }, "SalesTermRef": { "value": "3" }, "DueDate": "2020-04-02", "TotalAmt": 750, "ApplyTaxAfterDiscount": false, "PrintStatus": "NeedToPrint", "EmailStatus": "NotSet", "BillEmail": { "Address": "uiikoool" }, "Balance": 450 } let object = {} for (let k in invoiceObject) { if (typeof invoiceObject[k] === "object") { object[k] = {}; for (let l in invoiceObject[k]) { object[k][l] = ""; } } else if (typeof invoiceObject[k] === "array") { object[k] = []; for (let l in invoiceObject[k][0]) { object[k][l] = ""; } } else { object[k] = ""; } } console.log(object)
You can create a recursive function (getSchema) that checks if a value (val) is an object (arrays included), iterate it with _.flatMap(), and collects the keys until it hits a value which is not an object. It then joins the collected keys and returns the string. const getSchema = (val, keys = []) => _.isObject(val) ? // if it's an object or array _.flatMap(val, (v, k) => getSchema(v, [...keys, k])) // iterate it and call fn with the value and the collected keys : keys.join('.') // return the joined keys const invoiceObject = { "AllowIPNPayment": false, "AllowOnlinePayment": false, "AllowOnlineCreditCardPayment": false, "AllowOnlineACHPayment": false, "domain": "QBO", "sparse": false, "Id": "16", "SyncToken": "1", "MetaData": { "CreateTime": "2020-03-25T15:10:40-07:00", "LastUpdatedTime": "2020-03-26T11:06:49-07:00" }, "CustomField": [{ "DefinitionId": "1", "Name": "Crew #", "Type": "StringType" }], "DocNumber": "1007", "TxnDate": "2020-03-03", "CurrencyRef": { "value": "USD", "name": "United States Dollar" }, "LinkedTxn": [{ "TxnId": "32", "TxnType": "Payment" }], "Line": [{ "Id": "1", "LineNum": 1, "Description": "Custom Design", "Amount": 750, "DetailType": "SalesItemLineDetail", "SalesItemLineDetail": { "ItemRef": { "value": "4", "name": "Design" }, "UnitPrice": 75, "Qty": 10, "TaxCodeRef": { "value": "NON" } } }, { "Amount": 750, "DetailType": "SubTotalLineDetail", "SubTotalLineDetail": {} } ], "TxnTaxDetail": { "TotalTax": 0 }, "CustomerRef": { "value": "13", "name": "uiool" }, "CustomerMemo": { "value": "Thank you for your business and have a great day!" }, "SalesTermRef": { "value": "3" }, "DueDate": "2020-04-02", "TotalAmt": 750, "ApplyTaxAfterDiscount": false, "PrintStatus": "NeedToPrint", "EmailStatus": "NotSet", "BillEmail": { "Address": "uiikoool" }, "Balance": 450 } const result = getSchema(invoiceObject) console.log(result) <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.js"></script> Without lodash, the main change is to use Object.entries() to get an array of [key, value] pairs, since Array.flatMap() can't iterate objects: const getSchema = (val, keys = []) => typeof val === 'object' && val !== null ? // if it's an object or array Object.entries(val) // get [key, value] pairs of object/array .flatMap(([k, v]) => getSchema(v, [...keys, k])) // iterate it and call fn with the value and the collected keys : keys.join('.') // return the joined keys const invoiceObject = { "AllowIPNPayment": false, "AllowOnlinePayment": false, "AllowOnlineCreditCardPayment": false, "AllowOnlineACHPayment": false, "domain": "QBO", "sparse": false, "Id": "16", "SyncToken": "1", "MetaData": { "CreateTime": "2020-03-25T15:10:40-07:00", "LastUpdatedTime": "2020-03-26T11:06:49-07:00" }, "CustomField": [{ "DefinitionId": "1", "Name": "Crew #", "Type": "StringType" }], "DocNumber": "1007", "TxnDate": "2020-03-03", "CurrencyRef": { "value": "USD", "name": "United States Dollar" }, "LinkedTxn": [{ "TxnId": "32", "TxnType": "Payment" }], "Line": [{ "Id": "1", "LineNum": 1, "Description": "Custom Design", "Amount": 750, "DetailType": "SalesItemLineDetail", "SalesItemLineDetail": { "ItemRef": { "value": "4", "name": "Design" }, "UnitPrice": 75, "Qty": 10, "TaxCodeRef": { "value": "NON" } } }, { "Amount": 750, "DetailType": "SubTotalLineDetail", "SubTotalLineDetail": {} } ], "TxnTaxDetail": { "TotalTax": 0 }, "CustomerRef": { "value": "13", "name": "uiool" }, "CustomerMemo": { "value": "Thank you for your business and have a great day!" }, "SalesTermRef": { "value": "3" }, "DueDate": "2020-04-02", "TotalAmt": 750, "ApplyTaxAfterDiscount": false, "PrintStatus": "NeedToPrint", "EmailStatus": "NotSet", "BillEmail": { "Address": "uiikoool" }, "Balance": 450 } const result = getSchema(invoiceObject) console.log(result)
inspired by the answer given in this post and understanding you just want to get the property-names, not values, you could do it like this. sorry, this uses plain javascript. function flattenObjectToKeyArray(ob) { var toReturn = []; for (var prop in ob) { if (!ob.hasOwnProperty(prop)) continue; if ((typeof ob[prop]) == 'object' && ob[prop] !== null) { var flatObject = flattenObjectToKeyArray(ob[prop]); for (var idx = 0; idx < flatObject.length; idx++) { toReturn.push(prop + '.' + flatObject[idx]); } } else { toReturn.push(prop); } } return toReturn; }
You could solve this with a recursive function. The function below keeps track of the current keys, and joins them as soon as an end point is reached (a non-object or empty object/array). const invoiceObject = { "AllowIPNPayment": false, "AllowOnlinePayment": false, "AllowOnlineCreditCardPayment": false, "AllowOnlineACHPayment": false, "domain": "QBO", "sparse": false, "Id": "16", "SyncToken": "1", "MetaData": { "CreateTime": "2020-03-25T15:10:40-07:00", "LastUpdatedTime": "2020-03-26T11:06:49-07:00" }, "CustomField": [{ "DefinitionId": "1", "Name": "Crew #", "Type": "StringType" }], "DocNumber": "1007", "TxnDate": "2020-03-03", "CurrencyRef": { "value": "USD", "name": "United States Dollar" }, "LinkedTxn": [{ "TxnId": "32", "TxnType": "Payment" }], "Line": [{ "Id": "1", "LineNum": 1, "Description": "Custom Design", "Amount": 750, "DetailType": "SalesItemLineDetail", "SalesItemLineDetail": { "ItemRef": { "value": "4", "name": "Design" }, "UnitPrice": 75, "Qty": 10, "TaxCodeRef": { "value": "NON" } } }, { "Amount": 750, "DetailType": "SubTotalLineDetail", "SubTotalLineDetail": {} } ], "TxnTaxDetail": { "TotalTax": 0 }, "CustomerRef": { "value": "13", "name": "uiool" }, "CustomerMemo": { "value": "Thank you for your business and have a great day!" }, "SalesTermRef": { "value": "3" }, "DueDate": "2020-04-02", "TotalAmt": 750, "ApplyTaxAfterDiscount": false, "PrintStatus": "NeedToPrint", "EmailStatus": "NotSet", "BillEmail": { "Address": "uiikoool" }, "Balance": 450 }; function getDotKeys(item, keys = []) { const isObject = item && typeof item == "object"; if (!isObject) return Array.of(keys.join(".")); const pairs = Array.isArray(item) ? item.map((value, index) => [index, value]) : Object.entries(item); const isEmpty = !pairs.length; if (isEmpty) return Array.of(keys.join(".")); const result = []; for (const [key, value] of pairs) { const dotKeys = getDotKeys(value, [...keys, key]); result.push(...dotKeys); } return result; } console.log(getDotKeys(invoiceObject)); This does produce a different result than what you have in your question, since your solution stops at the second level for objects and third level for arrays. This solution also includes more then only index 0.
Javascript: Loop through nested objects and arrays
I have an server response with data which has the structur as you can see in the codesnippet. My goal is to iterate through each consent and add the channels The data from the response: [ { "consents": [ { "channels": [ { "granted": true, "id": "sms", "title": "SMS/MMS" }, { "granted": true, "id": "email", "title": "E-Mail" }, { "granted": false, "id": "phone", "title": "Telefon" }, { "granted": false, "id": "letter", "title": "Brief" } ], "client": "App", "configId": "99df8e86-2e24-4974-80da-74f901ba6a0d", "date": "2018-03-08T16:03:25.753Z", "granted": true, "name": "bestandsdaten-alle-produkte", "version": "1.0.0", "versionId": "bd002dcd-fee6-42f8-aafe-22d0209a0646" } ], "createdAt": "2018-03-08T16:03:25.778Z", "id": "1b9649a6-d8de-45c6-a0ae-a03cecf71cb5", "updatedAt": "2018-03-08T16:03:25.778Z", "username": "demo-app" }, { "consents": [ { "channels": [ { "granted": true, "id": "sms", "title": "SMS/MMS" }, { "granted": true, "id": "email", "title": "E-Mail" }, { "granted": true, "id": "phone", "title": "Telefon" }, { "granted": true, "id": "letter", "title": "Brief" } ], "client": "App", "configId": "99df8e86-2e24-4974-80da-74f901ba6a0d", "date": "2018-03-08T14:51:52.188Z", "granted": true, "name": "bestandsdaten-alle-produkte", "version": "1.0.0", "versionId": "bd002dcd-fee6-42f8-aafe-22d0209a0646" } ], "createdAt": "2018-03-08T14:51:52.208Z", "id": "cf550425-990e-45ef-aaee-eced95d8fa08", "updatedAt": "2018-03-08T14:51:52.208Z", "username": "demo-app" }, { "consents": [ { "channels": [ { "granted": false, "id": "sms", "title": "SMS/MMS" }, { "granted": true, "id": "email", "title": "E-Mail" }, { "granted": true, "id": "phone", "title": "Telefon" }, { "granted": false, "id": "letter", "title": "Brief" } ], "client": "App", "configId": "99df8e86-2e24-4974-80da-74f901ba6a0d", "date": "2018-03-08T14:48:27.024Z", "granted": true, "name": "bestandsdaten-alle-produkte", "version": "1.0.0", "versionId": "bd002dcd-fee6-42f8-aafe-22d0209a0646" } ], "createdAt": "2018-03-08T14:48:27.054Z", "id": "7fc1f087-2139-4494-bad7-161b0c6231a9", "updatedAt": "2018-03-08T14:48:27.054Z", "username": "demo-app" }, ] The way i go is the following. But i need a way to append the channels to each Consent. Is there a way to solve this? consentsList.forEach((consent, index, array) => { consent.consents.forEach((c) => { Object.keys(c).forEach((key) => { dd.content.push( { columns: [ { text: `${key}: `, style: 'text' }, { text: c[key], style: 'text' } ] } ); }); }); Consents.push(consent); if (index !== array.length - 1) { dd.content.push({ margin: [0, 0, 0, 5], canvas: [{ type: 'line', x1: 0, y1: 5, x2: 595 - (2 * 40), y2: 5, lineWidth: 0.6 }] }); } }); I want to output the individual channels where channels is on the top of each entry
You can extract the titles using map. And concantanate them using join like this. let value = ""; if(key === "channels"){ value = c[key].map(x => x.title).join(" ,"); } else { value = c[key]; } columns: [ { text: `${key}: `, style: 'text' }, { text: value, style: 'text' } ]
I have written a piece of code, hope this is what you're looking for. It is not optimized, try to optimize at your will var a = responseData; a.forEach(function(items) { var allChannels = []; items.consents.forEach(function(innerItems){ innerItems.channels.forEach(function(value){ allChannels.push(value.title); }) items.allChannels = allChannels.join(); }) }); console.log(a);
Fuzzy search deep array only on certain properties
I have a JSON dataset which could be very large when it returns, with the following structure for each object: { "ctr": 57, "averageECPC": 23, "cost": 2732.54, "margin": 66, "profit": 2495.9, "property": { "value": "Izzby", "uri": "/Terrago/2" }, "status": { "content": "<p>Some Content</p>", "stage": 1 }, "alerts": { "status": 2 }, "revenue": { "value": 2573.13, "compare": 0 }, "children": [{ "ctr": 79, "averageECPC": 54, "cost": 3554.78, "margin": 88, "profit": 3145.81, "property": { "value": "Comvex", "uri": "/Octocore/4" }, "status": { "content": "<p>Some Content</p>", "stage": 1 }, "alerts": { "status": 2 }, "revenue": { "value": 1247.92, "compare": 0 } }] } Now I want to search all objects in the array and return only objects which include a string of some sort, but I only want to search certain properties. I basically have another array which contains the keys I want to search, e.g. const iteratees = ['ctr', 'property.value', 'status.stage'] I have lodash available within the project, but I have no idea where to start. Any ideas?
You could use filter(), some() and reduce() to do this. const iteratees = ['ctr', 'property.value', 'status.stage']; var searchFor = 'lo'; var result = arr.filter(function(o) { return iteratees.some(function(e) { var res = e.split('.').reduce(function(a, b) { if(a) return a[b]; }, o); if(res) { if((res).toString().indexOf(searchFor) != -1) return true; } }) }) var arr = [{ "ctr": 'lorem', "averageECPC": 23, "cost": 2732.54, "margin": 66, "profit": 2495.9, "property": { "value": "Izzby", "uri": "/Terrago/2" }, "status": { "content": "<p>Some Content</p>", "stage": 1 }, "alerts": { "status": 2 }, "revenue": { "value": 2573.13, "compare": 0 }, "children": [{ "ctr": 79, "averageECPC": 54, "cost": 3554.78, "margin": 88, "profit": 3145.81, "property": { "value": "Comvex", "uri": "/Octocore/4" }, "status": { "content": "<p>Some Content</p>", "stage": 1 }, "alerts": { "status": 2 }, "revenue": { "value": 1247.92, "compare": 0 } }] }, { name: 'lorem', ctr: 12, property: { value: 1 }, status: { stage: 1 } }, { name: 'ipsum' }] const iteratees = ['ctr', 'property.value', 'status.stage']; var searchFor = 'lo'; var result = arr.filter(function(o) { return iteratees.some(function(e) { var res = e.split('.').reduce(function(a, b) { if (a) return a[b]; }, o); if (res) { if ((res).toString().indexOf(searchFor) != -1) return true; } }) }) console.log(result)