Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. const exception = {
  2. data: {
  3. error: {
  4. type: "Runtime Error",
  5. description: "asdf asdf asdf asdf",
  6. handled: true
  7. },
  8. extension: {
  9. response: {
  10. statusCode: 500,
  11. StatusText: "Service Unavailable"
  12. },
  13. exception: {},
  14. correlationId: "13287421389746123897467812364",
  15. stacktrace: []
  16. }
  17. }
  18. };
  19.  
  20. const whitelist = ["data.error", "data.extension.correlationId"];
  21.  
  22. export const filterObject = (originalObject, propertiesWhiteList) => {
  23. const newObject = {};
  24.  
  25. propertiesWhiteList.forEach(properties => {
  26. const propertyStack = properties.split(".");
  27. const maxDepth = propertyStack.length - 1;
  28.  
  29. let currentDepthNew = newObject;
  30. let currentDepthOriginal = originalObject;
  31.  
  32. propertyStack.forEach((property, depth) => {
  33. if (property in currentDepthOriginal) {
  34. if (depth === maxDepth) {
  35. currentDepthNew[property] = currentDepthOriginal[property];
  36. } else {
  37. if (!(property in currentDepthNew)) {
  38. currentDepthNew[property] = {};
  39. }
  40. currentDepthNew = currentDepthNew[property];
  41. currentDepthOriginal = currentDepthOriginal[property];
  42. }
  43. }
  44. });
  45. });
  46.  
  47. return newObject;
  48. };
  49.  
  50. const filteredObject = filterObject(exception, whitelist);
  51. console.log(filteredObject);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement