JavaScript Array.splice() 方法第一个参数(起始索引)

标签 javascript arrays methods

我在 MDN 中阅读了有关 .splice() 方法的内容。当我到达第一个参数(起始索引)时,我理解了除最后一个之外的所有情况。
“如果绝对值大于数组长度,则将设置为 0” 到底是什么意思?

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 (with origin -1) and will be set to 0 if absolute value is greater than the length of the array.

Link to the documentation

最佳答案

如果absolute value大于数组长度,它会执行与传递 0 相同的操作。

var arr = [0, 1, 2, 3, 4, 5, 6];
arr.splice(-8, 1); 
// Absolute value is 8, array length is 7
// Because 8 > 7, the first parameter is set to 0
// So this is the same as arr.splice(0, 1);

console.log(arr);

关于JavaScript Array.splice() 方法第一个参数(起始索引),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47643101/

相关文章:

javascript - THREE.js 三 Angular 形是黑色的

javascript - Fancybox2 使用 JWPlayer 6 将图像和视频放在同一组中

python - 在共享内存中使用 Multiprocessing.Array 时没有剩余空间

c++ - 在 C++ 中对数组进行洗牌和平方的函数的问题

java - 返回一个数字在数组中出现的次数?

Java方法isbn char转换和输出问题。无法找出算法中的错误

javascript - 表单验证 - Javascript 删除空格电话号码

javascript - 使用 CSS 和 wicket 操作 HTML 表格中 div 类的高度

php - 从关联数组中删除某些键和值

go - 了解结构嵌入