简体   繁体   English

JavaScript 拼接删除索引 + 1 处的数组元素而不是索引

[英]JavaScript splice removes array element at index + 1 instead of index

I'm seeing a weird issue where splice appears to remove an element at the index + 1 instead of the correct index.我看到一个奇怪的问题,splice 似乎删除了索引 + 1 处的元素,而不是正确的索引。 For instance, if selectedCourseMediaIndex = 0, then the element at index 1 in the array is removed.例如,如果 selectedCourseMediaIndex = 0,则删除数组中索引为 1 的元素。 Is there any particular reason why this is happening?发生这种情况有什么特别的原因吗?

  courseMedia: [
    {
      name: 'testing_startup_ideas.mp3',
      url:
        'https://s3.eu-west-2.amazonaws.com/media.test.co.uk/default/testing_startup_ideas.mp3',
      type: CourseContentMediaType.AUDIO,
    },
    {
      name: 'biggest_mistake_founders_make.mp3',
      url:
        'https://s3.eu-west-2.amazonaws.com/media.test.co.uk/default/biggest_mistake_founders_make.mp3',
      type: CourseContentMediaType.AUDIO,
    },
  ],

onEditAudioItemDeleteButtonClick() {
  let courseMedia = JSON.parse(JSON.stringify(this.courseMedia));
  courseMedia = CourseContentService.deleteCourseMediaByIndex(
    courseMedia,
    this.selectedCourseMediaIndex
  );
}

static deleteCourseMediaByIndex(
  courseMedia: ICourseMedia[],
  selectedCourseMediaIndex: number
): ICourseMedia[] {
  return courseMedia.splice(selectedCourseMediaIndex, 1);
}

splice does not return the updated array. splice不返回更新后的数组。

static deleteCourseMediaByIndex(
  courseMedia: ICourseMedia[],
  selectedCourseMediaIndex: number
): ICourseMedia[] {
  courseMedia.splice(selectedCourseMediaIndex, 1);
  return courseMedia
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM