0

Javascript: From the response of angular query build I get a response from it which looks like my array attribute,I need to convert it in my final_output out put in this array I get n number of condition & field with n number of depth.so I need to convert my array in to final_output with n number of depth.

Thanks in advance

Input 

let array = [
    {
        "query": {
            "condition": "and",
            "rules": [
                {
                    "field": "controlroom.isJurisdictional",
                    "operator": "=",
                    "entity": "controlroom"
                },
                {
                    "field": "controlroom.isJurisdictional",
                    "operator": "=",
                    "entity": "controlroom"
                },
                {
                    "condition": "and",
                    "rules": [
                        {
                            "field": "controlroom.isJurisdictional",
                            "operator": "=",
                            "entity": "controlroom"
                        },
                        {
                            "field": "controlroom.isJurisdictional",
                            "operator": "=",
                            "entity": "controlroom"
                        },
                        {
                            "condition": "and",
                            "rules": [
                                {
                                    "field": "controlroom.isJurisdictional",
                                    "operator": "=",
                                    "entity": "controlroom"
                                },
                                {
                                    "field": "controlroom.isJurisdictional",
                                    "operator": "=",
                                    "entity": "controlroom"
                                },
                                {
                                    "condition": "and",
                                    "rules": [
                                        {
                                            "field": "controlroom.isJurisdictional",
                                            "operator": "=",
                                            "entity": "controlroom"
                                        },
                                        {
                                            "field": "controlroom.isJurisdictional",
                                            "operator": "=",
                                            "entity": "controlroom"
                                        }
                                    ]
                                }
                            ]
                        }
                    ]
                }
            ]
        }
    }
]


And my final output looks like below

let final_output = [
    {
        "query": {
            must:[
                {
                    "field": "controlroom.isJurisdictional",
                    "operator": "=",
                    "entity": "controlroom"
                },
                {
                    "field": "controlroom.isJurisdictional",
                    "operator": "=",
                    "entity": "controlroom"
                },
                {
                    "bool": {
                        "must": [
                               {
                            "field": "controlroom.isJurisdictional",
                            "operator": "=",
                            "entity": "controlroom"
                        },
                        {
                            "field": "controlroom.isJurisdictional",
                            "operator": "=",
                            "entity": "controlroom"
                        },
                        {
                        "bool": {
                            "must": [ 
                                        {
                                            "field": "controlroom.isJurisdictional",
                                            "operator": "=",
                                            "entity": "controlroom"
                                        },
                                        {
                                            "field": "controlroom.isJurisdictional",
                                            "operator": "=",
                                            "entity": "controlroom"
                                        },
                                        {
                                            "bool": {
                                                    "must": [ 
                                                    {
                                                        "field": "controlroom.isJurisdictional",
                                                        "operator": "=",
                                                        "entity": "controlroom"
                                                    },
                                                    {
                                                        "field": "controlroom.isJurisdictional",
                                                        "operator": "=",
                                                        "entity": "controlroom"
                                                    }
                                                ]
                                            }
                                        }
                                    ]
                            }
                        }
                        ]
                    }
                }

               
            ]
        }
          
    }
]
1

1 Answer 1

2

You can use map function to achieve this here I have created a small demo for it.

 const queryData = {
        condition: "and",
        rules: [
          {
            field: "controlroom.isJurisdictional",
            operator: "=",
            entity: "controlroom",
          },
          {
            field: "controlroom.isJurisdictional",
            operator: "=",
            entity: "controlroom",
          },
          {
            condition: "and",
            rules: [
              {
                field: "controlroom.isJurisdictional",
                operator: "=",
                entity: "controlroom",
              },
              {
                field: "controlroom.isJurisdictional",
                operator: "=",
                entity: "controlroom",
              },
              {
                condition: "and",
                rules: [
                  {
                    field: "controlroom.isJurisdictional",
                    operator: "=",
                    entity: "controlroom",
                  },
                  {
                    field: "controlroom.isJurisdictional",
                    operator: "=",
                    entity: "controlroom",
                  },
                  {
                    condition: "and",
                    rules: [
                      {
                        field: "controlroom.isJurisdictional",
                        operator: "=",
                        entity: "controlroom",
                      },
                      {
                        field: "controlroom.isJurisdictional",
                        operator: "=",
                        entity: "controlroom",
                      },
                    ],
                  },
                ],
              },
            ],
          },
        ],
      };

      function convertToRulsHierarchyItem(rulslist) {
        if (rulslist === null || rulslist === undefined) return undefined;
        return rulslist.map((item) => {
          if (item.condition === "and") {
            return {
              bool: {
                must: convertToRulsHierarchyItem(item.rules),
              },
            };
          }
          if (item.condition === "or") {
            return {
              bool: {
                should: convertToRulsHierarchyItem(item.rules),
              },
            };
          }
          return item;
        });
      }
      const data = convertToRulsHierarchyItem(queryData.rules);
      console.log(data); 
Sign up to request clarification or add additional context in comments.

2 Comments

your own logic for it reduces code.
thanks for the answer it works fine for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.