可以使用'splice'方法来删除数组中的一个或多个元素。

语法:

array.splice(startIndex, deleteCount)

参数说明:

  • 'startIndex':要删除的元素的起始索引位置,必填。
  • 'deleteCount':要删除的元素数量,可选。如果不传递该参数,则会删除从'startIndex'位置到数组末尾的所有元素。

示例:

var arr = [1, 2, 3, 4, 5];
arr.splice(2, 1); // 删除索引为2的元素,即删除3
console.log(arr); // [1, 2, 4, 5]

arr.splice(1); // 从索引为1的位置开始删除所有元素
console.log(arr); // [1]
JavaScript 数组删除元素:splice 方法详解

原文地址: https://www.cveoy.top/t/topic/otXp 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录