Apologies for the complexity of this example; I've condensed it as much as possible to demonstrate what I'm aiming for
I have a complicated structure that needs to be traversed and transformed based on certain conditions. Here's a brief example of the structure that covers most scenarios:
{ PROP1: {
metadata: Object,
employee: {
id: String,
products: ["String", "String" , "String"],
model: { $ref: String },
email: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="cdac8dafe3aea2a0">[email protected]</a>',
name: { firstName: 'John', lastName: 'Smith'},
operations: [{ action: { value: "UPDATE", model: {$ref: String }}]
}
},
PROP2: {
// similar to PROP1
},
[... and so forth ...]
}
The desired outcome is to tidy up the structure before sending it to the backend;
- If a value contains
$ref
, remove the key/value pair (e.g., PROP1.parent is unnecessary) - If a value contains
value
, retain only the value under the corresponding key (e.g., PROP1.employee.products should equal ['String', 'String', 'String']) - Omit keys like
id
,metadata
,validity
, etc., regardless of their content
The expected result should resemble this structure:
{ PROP1: {
employee: {
products: ['item','item','item'],
model: {
email: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2c4d6c4e024f43441">[email protected]</a>',
name: { firstName: 'John', lastName: 'Smith'},
},
operations: [
{ action: 'UPDATE' }
]
}
},
PROP2: { ... }
}
I've tried various strategies using different lodash
methods but couldn't quite figure this out...
Any assistance would be highly appreciated
Thank you