【问题标题】:iterating a complex object, removing unwanted elements (multiple nested objects)迭代复杂对象,删除不需要的元素(多个嵌套对象)
【发布时间】:2019-10-04 18:01:35
【问题描述】:

这似乎是一项很容易的任务,但由于某种原因被卡住了!我尝试了我在SE中找到的几种解决方案,主要是递归迭代数组/对象,例如 How to iterate over an array and remove elements in JavaScriptLooping through array and removing items, without breaking for loop(以及许多其他人)但没有运气!

我有这个数组

{
  "condition": "AND",
  "rules": [
    {
      "id": "loc1",
      "type": "string",
      "input": "text",
      "operator": "equal",
      "value": "test0"
    },
    {
      "id": "sessionIgnore",
      "type": "string",
      "input": "text",
      "operator": "equal",
      "value": "ignore0"
    },
    {
      "condition": "AND",
      "rules": [
        {
          "id": "loc1",
          "type": "string",
          "input": "text",
          "operator": "equal",
          "value": "test1"
        },
        {
          "id": "sessionIgnore",
          "type": "string",
          "input": "text",
          "operator": "equal",
          "value": "ignore1"
        },
        {
          "condition": "AND",
          "rules": [
            {
              "id": "loc1",
              "type": "string",
              "input": "text",
              "operator": "equal",
              "value": "test2"
            },
            {
              "id": "sessionIgnore",
              "type": "string",
              "input": "text",
              "operator": "equal",
              "value": "ignore2"
            }
          ]
        }
      ]
    },
    {
      "condition": "AND",
      "rules": [
        {
          "id": "loc1",
          "type": "string",
          "input": "text",
          "operator": "equal",
          "value": "test3"
        },
        {
          "id": "sessionIgnore",
          "type": "string",
          "input": "text",
          "operator": "equal",
          "value": "ignore3"
        }
      ]
    }
  ],
  "valid": true
}

我想删除所有 sessionIgnore 元素

{
  "condition": "AND",
  "rules": [
    {
      "id": "loc1",
      "type": "string",
      "input": "text",
      "operator": "equal",
      "value": "test0"
    },
    {
      "condition": "AND",
      "rules": [
        {
          "id": "loc1",
          "type": "string",
          "input": "text",
          "operator": "equal",
          "value": "test1"
        },
        {
          "condition": "AND",
          "rules": [
            {
              "id": "loc1",
              "type": "string",
              "input": "text",
              "operator": "equal",
              "value": "test2"
            }
          ]
        }
      ]
    },
    {
      "condition": "AND",
      "rules": [
        {
          "id": "loc1",
          "type": "string",
          "input": "text",
          "operator": "equal",
          "value": "test3"
        }
      ]
    }
  ],
  "valid": true
}

在我的情况下似乎是什么问题: 在这两种情况下,我都会递归地遍历数组

  • delete x/array.slice() 方法: 似乎保留了对空数组元素的引用,然后我必须调用清理方法,我希望避免这种情况
  • 我创建了一个 arrayRemove() 函数,用于从给定数组中删除 x 元素,但在这种情况下,我似乎有一个逻辑问题,并且在数组的第二级之后我无法解析任何内容

感谢任何帮助!

编辑: 如果规则对象中只有一个 sessionIgnore 元素,我希望删除整个部分 即引用上述数组,如果值为 ignore2 的 sessionIgnore 是其“规则”组中的唯一对象,我希望删除整个部分 编辑2: 添加了预期的结果

编辑关于 nina 的对话:

如果对象是这样的:

{
      "condition": "AND",
      "rules": [
        {
          "id": "loc1",
          "type": "string",
          "input": "text",
          "operator": "equal",
          "value": "test1"
        },
        {
          "id": "sessionIgnore",
          "type": "string",
          "input": "text",
          "operator": "equal",
          "value": "ignore1"
        },
        {
          "condition": "AND",
          "rules": [
            {
              "id": "sessionIgnore",
              "type": "string",
              "input": "text",
              "operator": "equal",
              "value": "ignore2"
            }
          ]
        }
      ]
    }

我想要这个

{
      "condition": "AND",
      "rules": [
        {
          "id": "loc1",
          "type": "string",
          "input": "text",
          "operator": "equal",
          "value": "test1"
        }
      ]
    }

【问题讨论】:

  • 这不是一个数组 - 这是一个对象。
  • 您想要相同的对象,还是需要更改数据的新对象?
  • @VLAZ 但是属性"rules" 是一个数组。我猜 OP 指的是这个属性,而不是整个对象。
  • @PedroCorso 是的!正是
  • @NinaScholz 我不介意,只要我有一个没有 sessionIgnore 值的数组:D

标签: javascript arrays


【解决方案1】:

您可以通过检查id 来过滤rules,如果rules 存在,也可以过滤此属性。

function remove(o) {
    if (o.id === "sessionIgnore") return false;
    if (o.rules) o.rules = o.rules.filter(remove);
    return true;
}

var data = { condition: "AND", rules: [{ id: "loc1", type: "string", input: "text", operator: "equal", value: "test0" }, { id: "sessionIgnore", type: "string", input: "text", operator: "equal", value: "ignore0" }, { condition: "AND", rules: [{ id: "loc1", type: "string", input: "text", operator: "equal", value: "test1" }, { id: "sessionIgnore", type: "string", input: "text", operator: "equal", value: "ignore1" }, { condition: "AND", rules: [{ id: "loc1", type: "string", input: "text", operator: "equal", value: "test2" }, { id: "sessionIgnore", type: "string", input: "text", operator: "equal", value: "ignore2" }] }] }, { condition: "AND", rules: [{ id: "loc1", type: "string", input: "text", operator: "equal", value: "test3" }, { id: "sessionIgnore", type: "string", input: "text", operator: "equal", value: "ignore3" }] }], valid: true };

remove(data);

console.log(data);
.as-console-wrapper { max-height: 100% !important; top: 0; }

【讨论】:

  • 非常接近最终结果!并且比我的任何尝试都简单,但在我第一次编辑的情况下:如果规则中只有一个 sessionIgnore 元素,我希望删除整个父元素
  • 我不知道应该删除哪个部分?实际上我的结果看起来像你的结果......
  • 它确实:DI 没有附加另一个数组,因为问题已经很大了,如果包含 ignore2 和 test2 的元素只有 ignore2,我希望删除整个父元素,而不是一个元素"condition": "AND", "rules": [],换句话说,删除值为 test2 的元素并运行该数据集的解决方案 :)
  • 那么您需要为您的用例创建一个(小)数据集,将其与想要的结果一起添加到问题中。
  • 这没有意义,因为第一个示例没有返回任何内容,因为每个嵌套的 id: 'sessionIgnore' 都会导致更高级别的删除。基本上你只能得到第一个例子的结果,或者只能得到一个发送例子的结果,但不能同时得到。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-19
  • 1970-01-01
  • 1970-01-01
  • 2017-04-14
相关资源
最近更新 更多