I want to convert an object structure to a structure which the Json-rules-engine can use.
I have this array as input, where A, B, C, D and E are some arbitrary condition.
INPUT
[
{
operator: 'any',
conditions: [
{
operator: 'all',
conditions: [
{ operator: 'all', conditions: [ A, B ] },
{ operator: 'any', conditions: [ C, D ] }
]
},
E
]
}
]
I want to reach an output in the following structure:
{
any: [E,
{
all: [{
all: [A, B],
any: [C, D]
}],
}
]
}
I am pretty sure that I need a recursive function for this. I have already tried the following. The problem here is that the output is overwritten, while I want it to be expanded when the recursive function reaches deeper levels of the array.
recursive(input, output) {
input.forEach((el) => {
if (typeof el !== "number") {
output[el.operator] = el.conditions
this.recursive(el.conditions, output);
}
});
}
Thanks in advance!
[{op: 'any', conds: [A, B]}, {op: 'any', conds: [B, C]}]?