Stack Exchange
Nested multiple loop
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"
                                                    }
                                                ]
                                            }
                                        }
                                    ]
                            }
                        }
                        ]
                    }
                }

               
            ]
        }
          
    }
]


Familiarize yourself with how to access and process nested objects, arrays or JSON and how to create objects and use the available static and instance methods of Object and Array.

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); 


your own logic for it reduces code.

thanks for the answer it works fine for me.
