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
    Familiarize yourself with [how to access and process nested objects, arrays or JSON](/q/11922383/4642212) and how to [create objects](//developer.mozilla.org/docs/Web/JavaScript/Reference/Operators/Object_initializer) and use the available static and instance methods of [`Object`](//developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object#Static_methods) and [`Array`](//developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array#Static_methods). – Sebastian Simon Jan 25 '22 at 07:03

1 Answers1

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); 
Vineet
  • 70
  • 7