简体   繁体   English

splice 预计会删除多个元素,但只会删除一个

[英]splice expected to remove multiple elements, but only removes one

I'm using splice to remove the last n number of elements on an array (denoted by quizResponseCountToRemove ).我正在使用 splice 删除数组上的最后n个元素(由quizResponseCountToRemove表示)。

However, in its currently implementation, it only removes 1 element.但是,在其当前的实现中,它只删除了 1 个元素。

Is there any particular reason for this?这有什么特别的原因吗?

My code is as follows;我的代码如下;

  static removeCourseContentElementQuizResponseCount(
    course: ICourse,
    courseContents: ICourseContent[],
    selectedCourseContentUid: string,
    selectedCourseModule: number,
    quizResponseCountToRemove: number
  ): ICourse {
    courseContents.map((courseContent) => {
      if (courseContent.uid === selectedCourseContentUid) {
        courseContent.quiz.options.splice(-1, quizResponseCountToRemove);
        if (courseContent.quiz.answer > courseContent.quiz.options.length) {
          courseContent.quiz.answer = 1;
        }
      }
    });
    course.modules[selectedCourseModule].content = courseContents;
    return course;
  }

Update更新

It seems I have misunderstood how splice works.看来我误解了 splice 的工作原理。 I want splice to work backwards from the last element.我希望 splice 从最后一个元素向后工作。 So in an array ['test1', 'test2', 'test3', 'test4'] , if quizResponseCountToRemove is 2 then I end up with ['test1', 'test2'] .因此,在数组['test1', 'test2', 'test3', 'test4']中,如果quizResponseCountToRemove为 2,那么我最终得到['test1', 'test2']

If you pass splice a negative number for the first argument, that's an offset from the end of the array at which to perform the splice action.如果您为第一个参数传递一个负数splice ,则这是从执行拼接操作的数组末尾的偏移量。 You're passing -1 , so it'll perform its action on the last entry in the array.您正在传递-1 ,因此它将对数组中的最后一个条目执行其操作。 By definition, there's only one last entry in the array, so it doesn't matter how large quizResponseCountToRemove is.根据定义,数组中只有最后一个条目,因此quizResponseCountToRemove有多大并不重要。 If it's any number over 0 , only one element (the last one) will be removed.如果它是任何超过0的数字,则只会删除一个元素(最后一个)。

In a comment you've said:在评论中你说:

I want splice to work backwards from the last element.我希望 splice 从最后一个元素向后工作。 So in an array ['test1', 'test2', 'test3', 'test4'] , if quizResponseCountToRemove is 2 then I end up with ['test1', 'test2']所以在一个数组['test1', 'test2', 'test3', 'test4']中,如果quizResponseCountToRemove2那么我最终得到['test1', 'test2']

In that case, you want to start quizResponseCountToRemove from the end, so you could pass in -quizResponseCountToRemove :在这种情况下,您想从最后开始quizResponseCountToRemove ,因此您可以传入-quizResponseCountToRemove

courseContent.quiz.options.splice(-quizResponseCountToRemove, quizResponseCountToRemove);

...but that's unnecessarily complicated. ......但这太复杂了。 To truncate an array, you can just assign to its length :要截断数组,您可以分配给它的length

courseContent.quiz.options.length -= quizResponseCountToRemove;

If it's possible that quizResponseCountToRemove might be larger than the array's length, you'll need to ensure you don't try to assign a negative number to length (which will cause an error):如果quizResponseCountToRemove可能大于数组的长度,您需要确保不要尝试为length分配负数(这会导致错误):

courseContent.quiz.options.length = Math.max(0, courseContent.quiz.options.length - quizResponseCountToRemove);

Live Example:现场示例:

 const courseContent = { quiz: { options: ["test1", "test2", "test3", "test4"], }, }; const quizResponseCountToRemove = 2; console.log("before", JSON.stringify(courseContent.quiz.options)); courseContent.quiz.options.length -= quizResponseCountToRemove; console.log("after", JSON.stringify(courseContent.quiz.options));

You can use slice if you are looking to truncate an array.如果要截断数组,可以使用slice The problem with setting length of the array is that the length should be non-negative , so you need to add some checks.设置数组length的问题是length应该是非负的,所以你需要添加一些检查。

In code snippet below even if delCount is 200 , no additional checks are required slice will simply return an empty array.在下面的代码片段中,即使delCount200 ,也不需要额外的检查slice只会返回一个空数组。 But remember slice returns a new array.但请记住slice返回一个新数组。

 const arr = ["test1", "test2", "test3", "test4"], delCount = 2, newArr = arr.slice(0, -delCount); console.log(newArr)

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

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