Created
August 11, 2019 04:55
-
-
Save christian-schulze/1c116cb4ee1eedaef21eba8061e6aa55 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment