I have a specific array format that needs to undergo transformation.
{ [
{
"condition": "$and",
"children": [
{ "column": "Title", "comparison": "$eq", "columnValue": "1" },
{ "column": "Event Status", "comparison": "$eq", "columnValue": "2" }
]
},
{
"condition": "$or",
"children": [
{
"column": "Issue Description",
"comparison": "$lt",
"columnValue": "3"
},
{ "column": "Number Label", "comparison": "$gte", "columnValue": "4" }
]
}
]}
The desired transformed version is as follows...
{
[
{
"$and" : [
{
"Title" : {
"$eq" : "1"
}
},
{
"Event Status" : {
"$eq" : "2"
}
}
]
},
{
"$or" : [
{
"Issue Description" : {
"$lt" : "3"
}
},
{
"Number Label" : {
"$gte" : "4"
}
}
]
}
]
}
Several attempts using map and reduce methods have been made, coming close but not quite there yet.
This snippet belongs in a Vue project. See below for an example of the attempted code.
const result = this.parents.map(({ condition, children }) => {
const childArray = children.reduce(
(c, v) => ({
...c,
[v.column]: { [v.comparison]: v.columnValue }
}),
{}
);
childArray.condition = condition;
return childArray;
});
The current output is:
[
{
"Title": { "$eq": "1" },
"Event Status": { "$eq": "2" },
"condition": "$and"
},
{
"Issue Description": { "$lt": "3"},
"Number Label&”:: { "$gte&”:"4"},
"condition&”:& "$or"}
]
The challenge lies in correctly placing the "condition" key in the final result.