JavaScript Array.splice()方法的第一个参数(开始索引)(JavaScript Array.splice() method first argument(starting ind

系统教程 行业动态 更新时间:2024-06-14 16:52:52
JavaScript Array.splice()方法的第一个参数(开始索引)(JavaScript Array.splice() method first argument(starting index))

我正在阅读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); 
  
 

更多推荐

本文发布于:2023-04-05 12:19:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/7ab15a27f3db4c52bcfc98e9fe32c22d.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:第一个   索引   参数   方法   splice

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!