Nisha Patel
Query
How can I create a function that converts a dynamic Json to a formula in JavaScript?
Problem
I need to create a method that converts a dynamic Json that has this structure
where the keys "or", "and", "not" can be dynamic.
For example I can have this Json in input
and convert to this output:
as you can see the "and" operator in the above example needs to 'and' the two objects in array, the inner 'or' the two object in the array and the outer 'or' to concatenate the results of the inner 'and' and 'or'.
I tried to create a recursive method but it seems that doesn't work
export function convertToRule(operatoreLogico: OperatoreLogico, initial?: OperatoreLogico,
Solution
This should do what you're looking for:
That being said, this is a weird way to represent an AST, you might be better off with a structure like this:
Suggested blogs:
>How to use querySelectorAll()" with multiple conditions in JavaScript?
>How to fix mouseover event glitch in JavaScript?
>How to do light and dark mode in a website using HTML and JavaScript?
>How to manipulate manipulating Array object in JavaScript?
>How to merge an object into Array with the same key in JavaScript?
>Javascript Error Solved: Property 'id' does not exist on type 'T'
>Why highlighted table row using class not working in JavaScript?
>How to rename an object key based on the condition in JavaScript?
>How to sort an array based on another array in Javascript?
>Javascript: Modal not closing with a button