xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

js splice vs slice

js splice vs slice

splice

insert, replcae, add, delete


// let arrDeletedItems = array.splice(start[, deleteCount[, item1[, item2[, ...]]]])
const months = ['1', '2', '3'];

// insert, delete 0
months.splice(1, 0, '5');// ["1", "5", "2", "3"]
console.log(months);

// replace, delete 1
months.splice(3, 1, '6');// ["1", "5", "2", "6"]
console.log(months);

// add, delete -1
months.splice(4, 1, '7');// ["1", "5", "2", "6", "7"]
console.log(months);

// delete, delete 1
months.splice(4, 1);// ["1", "5", "2", "6"]
console.log(months);

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice

demo

https://stackoverflow.com/questions/37601282/javascript-array-splice-vs-slice

https://www.w3schools.com/jsref/jsref_slice_string.asp


let dataGET = [
    {
        index: 0,
        keyword: "a",
        value: "1",
        description: "a=1",
        operate: ["edit", "save", "cancel"],
        isEdit: false,
    },
    {
        index: 1,
        keyword: "b",
        value: "2",
        description: "b=1",
        operate: ["edit", "save", "cancel"],
        isEdit: false,
    },
    {
        index: 2,
        keyword: "c",
        value: "3",
        description: "c=1",
        operate: ["edit", "save", "cancel"],
        isEdit: false,
    },
];

// let arr = dataGET.slice(1, 1);

// console.log(arr);

//splice
var array=[1,2,3,4,5];
console.log(array.splice(2, 1));

//slice
var array2=[1,2,3,4,5]
console.log(array2.slice(2, 1));


console.log("----after-----");
console.log(array);
console.log(array2);



splice & delete Array item by index

index = 2


//splice & will modify the origin array
const arr1 = [1,2,3,4,5];
//slice & won't modify the origin array
const arr2 = [1,2,3,4,5]

console.log("----before-----");
console.log(arr1.splice(2, 1));
console.log(arr2.slice(2, 1));

console.log("----after-----");
console.log(arr1);
console.log(arr2);

refs



©xgqfrms 2012-2020

www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @   xgqfrms  阅读(225)  评论(8编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示