gpt4 book ai didi

javascript - 拼接删除最后一个元素

转载 作者:行者123 更新时间:2023-11-30 15:40:58 27 4
gpt4 key购买 nike

我试图用 splice 删除数组的一个元素,但是 .splice 一直在删除最后一个元素。我传递的索引是正确的。我做错了什么?

$scope.deleteSingleAnswer = function (index) {
console.log(index);
console.log($scope.editAnswers);
$scope.editAnswers.splice(index);
console.log($scope.editAnswers);
};

最佳答案

您需要指定在该特定索引之后要删除多少元素。看这里

The splice() method changes the content of an array by removing existing elements and/or adding new elements.

var myFish = ["angel", "clown", "mandarin", "surgeon"];
myFish.splice(2, 0, "drum");

Syntax

array.splice(start)
array.splice(start, deleteCount)
array.splice(start, deleteCount, item1, item2, ...)

Parameters

start Index at which to start changing the array (with origin 0). If greater than the length of the array, actual starting index will be set to the length of the array. If negative, will begin that many elements from the end of the array.

deleteCount Optional

An integer indicating the number of old array elements to remove. If deleteCount is 0, no elements are removed. In this case, you should specify at least one new element. If deleteCount is greater than the number of elements left in the array starting at start, then all of the elements through the end of the array will be deleted. If deleteCount is omitted, deleteCount will be equal to (arr.length - start). item1, item2, ... Optional The elements to add to the array, beginning at the start index. If you don't specify any elements, splice() will only remove elements from the array.

关于javascript - 拼接删除最后一个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40829050/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com