通过Node JS中的Object Literal Notation进行迭代和拆分

Iterating and splitting through Object Literal Notation in node JS

我是 Javascript 的新手。 我有以下 Json 数据。 输入

let data = {
        "filters": {
            "operator": "AND",
            "args": [
                {
                    "operator": "OR",
                    "args": [
                        {
                            "operator": "EQ",
                            "name": "consumer_id",
                            "args": [
                                "200"
                            ],
                            "type": "string"
                        },
                        {
                            "operator": "AND",
                            "args": [
                                {
                                    "operator": "RANGE",
                                    "name": "created_date",
                                    "args": [
                                        {"from":1},{"to":2}
                                    ],
                                    "type": "number"
                                },
                                {
                                    "operator": "EQ",
                                    "name": "state",
                                    "args": [
                                        "TN"
                                    ],
                                    "type": "string"
                                }
                            ]
                        }
                    ]
                },
                {
                    "operator": "GE",
                    "name": "Character",
                    "args": [
                        "H"
                    ],
                    "type": "string"
                }
            ]
    }

我正在尝试创建一个具有以下更改的新 Json。

  1. 将键名从名称替换为列。
  2. 找到运算符“RANGE”后,使用 LE 和 GE 将其拆分为 2 个运算符。

预期输出

{
  "filters": {
    "operator": "AND",
    "args": [
      {
        "operator": "OR",
        "args": [
          {
            "operator": "EQ",
            "column": "consumer_id",
            "args": [
              "200"
            ],
            "type": "string"
          },
          {
            "operator": "AND",
            "args": [
              {
                "operator": "LE",
                "column": "created_date",
                "args": [
                  1
                ],
                "type": "number"
              },
              {
                "operator": "GE",
                "column": "created_date",
                "args": [
                  2
                ],
                "type": "number"
              },
              {
                "operator": "EQ",
                "column": "state",
                "args": [
                  "TN"
                ],
                "type": "string"
              }
            ]
          }
        ]
      },
      {
        "operator": "GE",
        "column": "character",
        "args": [
          "H"
        ],
        "type": "string"
      }
    ]
  }
}

我的代码:

data.filters.args.filter( item => {
    iterateObject(item);
});
function iterateObject(obj) {
    for(var prop in obj) {
      if(typeof(obj[prop]) === "object" ) {
        iterateObject(obj[prop]);
    } else {
       if (prop === "operator" && obj[prop] === "RANGE") {
           obj={
              "LE": {"operator": "LE",
               "column": obj.name,
                "args": [obj.args[0].from],
                "type": obj.type
             },
            "GE":{
                "operator": "GE",
                "column": obj.name,
                "args": [obj.args[1].to],
                "type": obj.type
             }
           }
           console.log(obj) // The object gets replaced here, but I don't know how to map with original Json( ie, to the variable data )
       }
       if (prop === "name" ) {
           prop="column"
       } 
    }
  }
}
console.log(JSON.stringify(data));

问题:

我能够成功实现改变1 我尝试了多种方法来实现更改 2,但我做不到。请帮忙

这应该可以解决问题:

  1. 检查 name 属性 的对象;如果存在,将其复制到 column 属性 并删除 obj.name
  2. 检查 obj.operator === 'RANGE';如果 true
    1. operator 重命名为 AND
    2. 创建两个新的单操作对象并将其添加到args
    3. 删除 columntype 字段

例子

let data = {
  "filters": {
    "operator": "AND",
    "args": [
      {
        "operator": "OR",
        "args": [
          {
            "operator": "EQ",
            "name": "consumer_id",
             "args": [
               "200"
             ],
             "type": "string"
           },
           {
             "operator": "AND",
             "args": [
               {
                 "operator": "RANGE",
                 "name": "created_date",
                 "args": [
                   {"from":1},{"to":2}
                 ],
                 "type": "number"
               },
               {
                 "operator": "EQ",
                 "name": "state",
                 "args": [
                   "TN"
                 ],
                 "type": "string"
               }
             ]
           }
         ]
       },
       {
         "operator": "GE",
         "name": "Character",
         "args": [
           "H"
         ],
         "type": "string"
       }
     ]
   }
};


function modify(obj) {
  if (!obj) return;
  
  if (obj.name) {
    obj.column = obj.name
    delete obj.name;
  }
  
  if (obj.operator === 'RANGE') {    
    obj.operator = 'AND';
    obj.args = [
      {
        "operator": "GE",
        "column": obj.column,
        "args": [ obj.args[0].from ],
        "type": obj.type
      },
      {
        "operator": "LE",
        "column": obj.column,
        "args": [ obj.args[1].to ],
        "type": obj.type
      }
    ];
    
    delete obj.column;
    delete obj.type;
  }
  
  for (const arg of obj.args || []) {
    modify(arg);
  }
}


modify(data.filters);
console.log(JSON.stringify(data, null, 2));