Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const exception = {
- data: {
- error: {
- type: "Runtime Error",
- description: "asdf asdf asdf asdf",
- handled: true
- },
- extension: {
- response: {
- statusCode: 500,
- StatusText: "Service Unavailable"
- },
- exception: {},
- correlationId: "13287421389746123897467812364",
- stacktrace: []
- }
- }
- };
- const whitelist = ["data.error", "data.extension.correlationId"];
- export const filterObject = (originalObject, propertiesWhiteList) => {
- const newObject = {};
- propertiesWhiteList.forEach(properties => {
- const propertyStack = properties.split(".");
- const maxDepth = propertyStack.length - 1;
- let currentDepthNew = newObject;
- let currentDepthOriginal = originalObject;
- propertyStack.forEach((property, depth) => {
- if (property in currentDepthOriginal) {
- if (depth === maxDepth) {
- currentDepthNew[property] = currentDepthOriginal[property];
- } else {
- if (!(property in currentDepthNew)) {
- currentDepthNew[property] = {};
- }
- currentDepthNew = currentDepthNew[property];
- currentDepthOriginal = currentDepthOriginal[property];
- }
- }
- });
- });
- return newObject;
- };
- const filteredObject = filterObject(exception, whitelist);
- console.log(filteredObject);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement