从嵌套对象中删除某些对象的最佳方法是什么?

what is the best way to remove certain objects from a nested object?

我有以下嵌套对象:


{
  "cards": {
    "1": {
      "x": 247,
      "y": 213,
      "properties": {
        "id": 1,
        "name": "a",
        "intros": {
          "intro_0": {
            "id": 1,
            "cardId": 1
          },
          "intro_1": {
            "id": 2,
            "cardId": 1
          },
          "intro_3": {
            "id": 24,
            "cardId": 1
          }
        },
        "exits": {
          "exit_0": {
            "id": 1,
            "cardId": 1
          },
          "exit_1": {
            "id": 22,
            "cardId": 1            
          }
        },
        "mixins": {
          "mixin_0": {
            "id": 23,
            "cardId": 1
          }
        }
      }
    },
    "3": {
      "x": 762,
      "y": 187,
      "properties": {
        "id": 1,
        "name": "x",
        "intros": {
          "intro_0": {
            "id": 1,
            "cardId": 1
          },
          "intro_1": {
            "id": 263,
            "cardId": 1
          }
        },
        "exits": {
          "exit_0": {
            "id": 1,
            "cardId": 1
          },
          "exit_1": {
            "id": 2,
            "cardId": 1
          }
        },
        "mixins": {
          "mixin_0": {
            "id": 3,
            "cardId": 1
          }
        },
      }
    },
    "4": {
      "x": 1200,
      "y": 187,
      "properties": {
        "id": 1,
        "name": "j",
        "intros": {
          "intro_0": {
            "id": 1,
            "cardId": 1
          },
          "intro_1": {
            "id": 276,
            "cardId": 1
          }
        },
        "exits": {
          "exit_0": {
            "id": 1,
            "cardId": 1
          },
          "exit_1": {
            "id": 2,
            "cardId": 1
          }
        },
        "mixins": {
          "mixin_0": {
            "id": 3,
            "cardId": 1
          }
        }
      }
    }
  }
}

我正在尝试遍历对象以仅提取 "intro" 中的第一个和 "exit" 中的第一个并修改原始对象,以便它只包含这些和其余的数据。或者相同的是,我想删除除第一个以外的所有 "intros" 和除第一个以外的所有 "exits" 。 为了提取我想要的值,我为 in ... 制作了 3,但是当谈到修改原始对象时,我认为我把它弄得太复杂了,必须有一些更简单的方法。

const cards: any = Object.values(data.cards);

      for (const indexCard in cards) {
        if (cards.hasOwnProperty(indexCard)) {
          const card = cards[indexCard];
          const cardIntros = card.properties.intros;
          for (const introIndex in cardIntros) {
            if (cardIntros.hasOwnProperty(introIndex) && introIndex === 'intro_0') {
              const firstIntro = cardIntros[introIndex];
              const cardsExits = card.properties.exits;
              for (const exitIndex in cardsExits) {
                if (cardsExits.hasOwnProperty(exitIndex) && exitIndex === 'exit_0') {
                  const firstExit = cardsExits[exitIndex];
                }
              }

            }
          }
        }
      }

有没有人看到更简单的方法来删除我想删除的 "intros" 和 "exits",以便生成一个只包含所需 "intros" 和 "exits"?

提前致谢!

您可以创建递归函数来检查当前值是否为对象,然后检查该对象的某些键是否等于参数提供的键。如果它确实包含该键,它将从该对象中删除其他键。这适用于任何级别的嵌套。

const data = {"cards":{"1":{"x":247,"y":213,"properties":{"id":1,"name":"a","intros":{"intro_0":{"id":1,"cardId":1},"intro_1":{"id":2,"cardId":1},"intro_3":{"id":24,"cardId":1}},"exits":{"exit_0":{"id":1,"cardId":1},"exit_1":{"id":22,"cardId":1}},"mixins":{"mixin_0":{"id":23,"cardId":1}}}},"3":{"x":762,"y":187,"properties":{"id":1,"name":"x","intros":{"intro_0":{"id":1,"cardId":1},"intro_1":{"id":263,"cardId":1}},"exits":{"exit_0":{"id":1,"cardId":1},"exit_1":{"id":2,"cardId":1}},"mixins":{"mixin_0":{"id":3,"cardId":1}}}},"4":{"x":1200,"y":187,"properties":{"id":1,"name":"j","intros":{"intro_0":{"id":1,"cardId":1},"intro_1":{"id":276,"cardId":1}},"exits":{"exit_0":{"id":1,"cardId":1},"exit_1":{"id":2,"cardId":1}},"mixins":{"mixin_0":{"id":3,"cardId":1}}}}}}

function first(data, props = []) {
  return Object.entries(data).reduce((r, [k, v]) => {
    if (typeof v == 'object' && v != null) {
      if (Array.isArray(v)) {
        r[k] = v.map(e => first(e, props))
      } else {
        const value = { ...v }
        const check = props.some(p => Object.keys(v).includes(p))

        if (check) {
          for (let i in value) {
            if (!props.includes(i)) {
              delete value[i]
            }
          }
        }

        r[k] = first(value, props)
      }

    } else r[k] = v
    return r;
  }, {})
}


const result = first(data, ['intro_0', 'exit_0'])
console.log(result)