我在 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.
最佳答案
如果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/