我有一个具有以下结构的对象数组:
[
{
"id": 1,
"uid": "card-vwy733s3rC",
"body": {
"text": "Test"
},
"type": 0,
"button": [
{
"uid": "button-tFDHsmExnx",
"title": "Get Started",
"event": [
{
"id": 2,
"action": 5,
"value": "https://en.wikipedia.org/wiki/Educational_technology"
},
{
"id": 2,
"action": 5,
"value": "https://en.wikipedia.org/wiki/Educational_technology"
}
],
"isEnabled": true
}
],
"audio": {
"uid": "audio-NVBK0U6BXE",
"url": "https://s3.eu-west-2.amazonaws.com/media.testing.co.uk/default/testing_startup_ideas.mp3"
}
}
]
button -> event -> id
有时,提供最顶级的uid匹配。这意味着两个2变成了1和2。但是,当我在下面运行我的方法时,它不起作用,并且它会为
button -> event
数组:
static resetCourseContentButtonEventIndexes(
courseContent: ICourseContent[],
selectedCourseContentUid: string
): ICourseContent[] {
for (let i = 0; i < courseContent.length; i++) {
if (courseContent[i].uid === selectedCourseContentUid) {
for (let j = 0; j < courseContent[i].button.length; j++) {
for (let k = 0; k < courseContent[i].button[j].event.length; k++) {
courseContent[i].button[j].event[k].id = k + 1;
}
}
}
}
return courseContent;
}
我很困惑为什么会发生这种情况,有人能提供一些关于如何解决它的建议吗?
我从另一个方法调用它:
static cloneCourseContentButtonEventByIndex(
courseContent: ICourseContent[],
selectedCourseContentUid: string,
selectedCourseElementUid: string,
courseContentButtonEventIndex: number
) {
for (let i = 0; i < courseContent.length; i++) {
if (courseContent[i].uid === selectedCourseContentUid) {
for (let j = 0; j < courseContent[i].button.length; j++) {
if (courseContent[i].button[j].uid === selectedCourseElementUid) {
courseContent[i].button[j].event.push(
courseContent[i].button[j].event[courseContentButtonEventIndex]
);
}
}
}
}
return JSON.parse(
JSON.stringify(
this.resetCourseContentButtonEventIndexes(courseContent, selectedCourseContentUid)
)
);
}