首页 > 解决方案 > 如何拼接第 0 个索引下的每个“条件”特定键“逻辑”和嵌套 JSON 的值?

问题描述

我有一个嵌套对象,我想在每个“条件”中找到拼接:第 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"
          },
{
            "logic": "AND",
            "parameter": "Risk Engine Score",
            "condition": "Equals",
            "value": "35645",
            "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"
          },
          {
            "logic": "AND",
            "parameter": "Risk Engine Score",
            "condition": "Equals",
            "value": "345",
            "level": "second",
            "type": "condition"
          }

        ],
        "groupLogic": "AND"
      }
    ],
    "modeOfAuth": "frictionless"
  }
]

预期输出:

[
  {
    "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"
          },
{
            "logic": "AND",
            "parameter": "Risk Engine Score",
            "condition": "Equals",
            "value": "35645",
            "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": [
          {
            "parameter": "Amount",
            "condition": "< Less than",
            "value": "12",
            "level": "second",
            "type": "condition"
          },
          {
            "logic": "AND",
            "parameter": "Risk Engine Score",
            "condition": "Equals",
            "value": "345",
            "level": "second",
            "type": "condition"
          }

        ],
        "groupLogic": "AND"
      }
    ],
    "modeOfAuth": "frictionless"
  }
]

代码:

const parseData = (data) => data.map(({conditions, ...rest}) => conditions.map(({logic, ...conditions}) => ({conditions, ...rest})));

console.log(parseData(data));

试过上面的代码。所有索引中的上述 JSON 对象仅需要删除第 0 个索引“逻辑”键下的每个条件

标签: javascriptarraysjsonreactjsloops

解决方案


var recursive = b =>
  b.map((val, i) => {
    if (i === 0) delete val.logic;
    if (val.conditions) {
      val.conditions = recursive(val.conditions);
    }
    return val;
  });

像这样的东西应该可以工作,如果它是数组的第一个元素,我只是删除它,并且还为这些对象内logic的每个嵌套数组递归调用此函数。conditions


推荐阅读