I have a data coming from API as follows -
{
"expression": [
"a",
"b"
],
"groups": [
{
"expression": [
],
"groups": [
{
"expression": [
"c",
"d"
],
groups: null
}
]
},
{
"expression": [
"p",
],
groups: null
}
]
}
I have to render Expressions and Groups (that includes expressions and multiple groups as well). The building block is an expression. We need to display nested expressions as groups.
I tried with recursive function call, but that is not working with much efficiency for an object with large depth.
Here is something similar to my trial -
let conditionStack = [];
function parseInnerConditions(conditionObj) {
if (conditionObj.expression) {
conditionObj.expression.map((each, index) => {
conditionStack = [
...conditionStack,
<NewExpression key={index} conditionExpression={each} />, //renders an expression
];
});
}
if (conditionObj.groups && conditionObj.groups.length) {
return conditionObj.groups.map((child, index) => {
conditionStack = [
...conditionStack,
<ConditionGroup key={index}> // renders a block or group of expressions
{parseInnerConditions(child)}
</ConditionGroup>,
];
});
}
return conditionStack;
}
return (
<div>
parseInnerConditions(conditionObject)
</div>
)
Thanks in advance.