slice&splice使用

Slice array elements

Slice: To cut out a multiple items in range. It takes two parameters: starting and ending position
It doesn't inclue the ending position.

const numbers = [1, 2, 3, 4, 5]
console.log(numbers.slice()); // it copies all item
console.log(numbers.slice(0)); // it copies all item
console.log(numbers.slice(0, numbers.length)); // it copies all item
console.log(numbers.slice(1, 4)) // -> [2, 3, 4] it doesn't include the ending position

Splice methond in array

Splice: It takes three parameters: Starting position, number of times to be removed and number of items to be added.

const numbers = [1, 2, 3, 4, 5]
console.log(numbers.splice()); // remove all items
console.log(numbers.splice(0, 1)); // remove the first item
posted @   Felix_Openmind  阅读(4)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· 写一个简单的SQL生成工具
*{cursor: url(https://files-cdn.cnblogs.com/files/morango/fish-cursor.ico),auto;}
点击右上角即可分享
微信分享提示