我有一个嵌套的对象,我想在其中的每个第0个索引键“逻辑”和值中找到接头。假设对象看起来像这样:

原始输入:

[
  {
    "conditions": [
      {
        "logic": "AND",
        "parameter": "Risk Engine Score",
        "condition": "Equals",
        "value": "122",
        "level": "first",
        "type": "condition"
      },
      {
        "level": "second",
        "type": "group",
        "nextChildLogic": "AND",
        "conditions": [
          {
            "logic": "AND",
            "parameter": "Risk Engine Score",
            "condition": "Equals",
            "value": "123",
            "level": "second",
            "type": "condition"
          }
        ],
        "groupLogic": "AND"
      }
    ],
    "modeOfAuth": "otp"
  },
  {
    "conditions": [
      {
        "logic": "AND",
        "parameter": "Risk Engine Score",
        "condition": "< Less than",
        "value": "12",
        "level": "first",
        "type": "condition"
      }
    ],
    "modeOfAuth": "frictionless"
  },
  {
    "conditions": [
      {
        "logic": "AND",
        "parameter": "Risk Engine Score",
        "condition": "Equals",
        "value": "12",
        "level": "first",
        "type": "condition"
      },
      {
        "level": "second",
        "type": "group",
        "nextChildLogic": "AND",
        "conditions": [
          {
            "logic": "AND",
            "parameter": "Amount",
            "condition": "< Less than",
            "value": "12",
            "level": "second",
            "type": "condition"
          }
        ],
        "groupLogic": "AND"
      }
    ],
    "modeOfAuth": "frictionless"
  }
]


码:

for(var i=0;i〈conditionItem.length;i++)
    {
      // conditionItem[0].splice(conditionItem.indexOf(logic), 1);


      // console.log("ccc=",conditionItem)
      if(Array.isArray(conditionItem[i].condition))
      {
        var conditionItem1=[];
        for(var j=0;j〈conditionItem.length;j++)
        {
          if(j==0)
          {
            conditionItem1.push({"condition":conditionItem[j].condition,"level":conditionItem[j].level,"parameter":conditionItem[j].parameter,"type":conditionItem[j].type,"value":conditionItem[j].value})
          }else{
            conditionItem1.push(conditionItem[j])
          }

        }
        conditionItem[i].condition = conditionItem1;
      }

    }
    console.log("conditionItem::=",conditionItem);

for(var i=0;i〈conditionItem.length;i++)
    {
      // conditionItem[0].splice(conditionItem.indexOf(logic), 1);


      // console.log("ccc=",conditionItem)
      if(Array.isArray(conditionItem[i].condition))
      {
        var conditionItem1=[];
        for(var j=0;j〈conditionItem.length;j++)
        {
          if(j==0)
          {
            conditionItem1.push({"condition":conditionItem[j].condition,"level":conditionItem[j].level,"parameter":conditionItem[j].parameter,"type":conditionItem[j].type,"value":conditionItem[j].value})
          }else{
            conditionItem1.push(conditionItem[j])
          }

        }
        conditionItem[i].condition = conditionItem1;
      }

    }
    console.log("conditionItem::=",conditionItem);


预期输出:

[
  {
    "conditions": [
      {

        "parameter": "Risk Engine Score",
        "condition": "Equals",
        "value": "122",
        "level": "first",
        "type": "condition"
      },
      {
        "level": "second",
        "type": "group",
        "nextChildLogic": "AND",
        "conditions": [
          {

            "parameter": "Risk Engine Score",
            "condition": "Equals",
            "value": "123",
            "level": "second",
            "type": "condition"
          }
        ],
        "groupLogic": "AND"
      }
    ],
    "modeOfAuth": "otp"
  },
  {
    "conditions": [
      {
        "parameter": "Risk Engine Score",
        "condition": "< Less than",
        "value": "12",
        "level": "first",
        "type": "condition"
      }
    ],
    "modeOfAuth": "frictionless"
  },
  {
    "conditions": [
      {

        "parameter": "Risk Engine Score",
        "condition": "Equals",
        "value": "12",
        "level": "first",
        "type": "condition"
      },
      {
        "level": "second",
        "type": "group",
        "nextChildLogic": "AND",
        "conditions": [
          {

            "parameter": "Amount",
            "condition": "< Less than",
            "value": "12",
            "level": "second",
            "type": "condition"
          }
        ],
        "groupLogic": "AND"
      }
    ],
    "modeOfAuth": "frictionless"
  }
]

最佳答案

我能想到的最优雅,最简短的方法是使用map和一些递归:

// setting a default empty array in case the *initial* object has no condition key
const parseData = ({ conditions = [], ...rest }) => {
  const cond = conditions.map(({ logic, ...condition }) => {
    if (condition.conditions) {
      return parseData(condition);
    }
    return condition;
  });

  return {
    conditions: cond,
    ...rest
  };
};


分解:


遍历每个元素
destructuring(提取)conditions,同时将其余部分保留在rest
遍历提取的conditions
以与#2中相同的方式提取logic
如果我们的条件有其自身的条件-我们将再次调用parseData,否则-使用spread syntax结合conditions减去提取的logic以及先前未提取到rest的所有其他条件,返回一个新对象


@ 3limin4t0r指出的重要说明:对象剩余销毁分配目前仍是第3阶段的提议。
它已经获得主要浏览器的支持,但是规格可能会发生变化。
以下是有关此事的一些想法:https://stackoverflow.com/a/48150001/1194694

一个工作示例:



const data = [ { "conditions": [ { "logic": "AND", "parameter": "Risk Engine Score", "condition": "Equals", "value": "122", "level": "first", "type": "condition" }, { "level": "second", "type": "group", "nextChildLogic": "AND", "conditions": [ { "logic": "AND", "parameter": "Risk Engine Score", "condition": "Equals", "value": "123", "level": "second", "type": "condition" } ], "groupLogic": "AND" } ], "modeOfAuth": "otp" }, { "conditions": [ { "logic": "AND", "parameter": "Risk Engine Score", "condition": "< Less than", "value": "12", "level": "first", "type": "condition" } ], "modeOfAuth": "frictionless" }, { "conditions": [ { "logic": "AND", "parameter": "Risk Engine Score", "condition": "Equals", "value": "12", "level": "first", "type": "condition" }, { "level": "second", "type": "group", "nextChildLogic": "AND", "conditions": [ { "logic": "AND", "parameter": "Amount", "condition": "< Less than", "value": "12", "level": "second", "type": "condition" } ], "groupLogic": "AND" } ], "modeOfAuth": "frictionless" } ]

const parseData = ({ conditions = [], ...rest }) => {
  const cond = conditions.map(({ logic, ...condition }) => {
    if (condition.conditions) {
      return parseData(condition);
    }
    return condition;
  });

  return {
    conditions: cond,
    ...rest
  };
};

console.log(data.map(parseData));

10-07 17:28