我正在阅读MDN中的.splice()方法。 当我到达第一个参数(起始索引)时,我理解了所有情况,但最后一个。 “如果绝对值大于数组的长度,它将被设置为0”究竟意味着什么?
参数
开始 开始更改数组的索引(原点为0)。 如果大于数组的长度,实际的起始索引将设置为数组的长度。 如果为负数,则会从数组末尾开始处理许多元素(原点-1) ,如果绝对值大于数组的长度,则会将其设置为0。
链接到文档
I was reading about .splice() method in MDN. When I got to the first parameter(starting index) I understood all cases but the last one. What does "and will be set to 0 if absolute value is greater than the length of the array" exactly means?
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
最满意答案
如果绝对值大于数组长度,它将执行与传递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);If the absolute value is greater than the array length it will do the same thing as if you passed 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);
更多推荐
发布评论