我有以下嵌套对象:
{
"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”中的第一个,并修改原始对象,使其仅包含这些数据和其余数据。或者相同的是,我想删除除第一个之外的所有“介绍”和除第一个之外的所有“退出”。为了提取我想要的值,我为 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];
}
}
}
}
}
}
有没有人看到一种更简单的方法来删除我想要删除的“介绍”和“退出”,以便生成一个只包含所需“介绍”和“退出”的新对象?
先感谢您!