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, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/10711789.html
未经授权禁止转载,违者必究!