xxxxxxxxxx
var array1= ['S', 'T', 'A', 'C', 'K', 'O', 'V', 'E', 'R', 'F', 'L', 'O', 'W'];
// At position 3, delete 7 and add 7 elements:
array1.splice(3, 7, "X","X","X","X","X","X","X");
console.log(array1);
xxxxxxxxxx
let arr = ['foo', 'bar', 10, 'qux'];
// arr.splice(<index>, <steps>, [elements ...]);
arr.splice(1, 1); // Removes 1 item at index 1
// => ['foo', 10, 'qux']
arr.splice(2, 1, 'tmp'); // Replaces 1 item at index 2 with 'tmp'
// => ['foo', 10, 'tmp']
arr.splice(0, 1, 'x', 'y'); // Inserts 'x' and 'y' replacing 1 item at index 0
// => ['x', 'y', 10, 'tmp']
xxxxxxxxxx
/*splice(start)
splice(start, deleteCount)
splice(start, deleteCount, replaceitem1)
splice(start, deleteCount, replaceitem1, ""2, ""N) */
//examples
let name = ['Jon', 'smith', 'pete', 'mary'];
name.splice(1, 0, 'hunt');
// inserts at index 1
console.log(name);
xxxxxxxxxx
const months = ['Jan', 'March', 'April', 'June'];
months.splice(1, 0, 'Feb');
// inserts at index 1
console.log(months);
// expected output: Array ["Jan", "Feb", "March", "April", "June"]
months.splice(4, 1, 'May');
// replaces 1 element at index 4
console.log(months);
// expected output: Array ["Jan", "Feb", "March", "April", "May"]
months.splice(0, 1);
// removes 1 element at index 0
console.log(months);
// expected output: Array ["Feb", "March", "April", "May"]
xxxxxxxxxx
The splice() method adds and/or removes array elements.
The splice() method overwrites the original array.
array.splice(index, howmany to remove, item1, .., itemX)
const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.splice(2, 1, "Lemon", "Kiwi");
//Banana,Orange,Lemon,Kiwi,Mango
xxxxxxxxxx
/*splice(start)
splice(start, deleteCount)
splice(start, deleteCount, replaceitem1)
splice(start, deleteCount, replaceitem1, ""2, ""N) */
//examples
const months = ['Jan', 'March', 'April', 'June'];
months.splice(1, 0, 'Feb');
// inserts at index 1
console.log(months);
// expected output: Array ["Jan", "Feb", "March", "April", "June"]
months.splice(4, 1, 'May');
// replaces 1 element at index 4
console.log(months);
// expected output: Array ["Jan", "Feb", "March", "April", "May"]
xxxxxxxxxx
// The splice() method can be used to add new items to an array:
const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.splice(2, 0, "Lemon", "Kiwi");
// >> ["Banana", "Orange", "Lemon", "Kiwi", "Apple", "Mango"]
//if you find this answer is useful ,
//upvote ⇑⇑ , so can the others benefit also . @mohammad alshraideh ( ͡~ ͜ʖ ͡°)
xxxxxxxxxx
let fruits = ["apple", "pear", "plum", "orange", "cherry"];
fruits.splice(1, 0, 'watermelon');
console.log(fruits);
fruits.splice(1, 1);
console.log(fruits)
/*
[ 'apple', 'watermelon', 'pear', 'plum', 'orange', 'cherry' ]
[ 'apple', 'pear', 'plum', 'orange', 'cherry' ]
*/
xxxxxxxxxx
const arrayToBeFixed = ['a', 'b', 'wrong', 'f']
const removedElements = arrayToBeFixed.splice(2, // Where to insert
1, // number of elements to be removed from arrayToBeFixed
'c', 'd', 'e', // elements to add starting from the insertion index
);
console.log(arrayToBeFixed) // ['a' 'b', 'c', 'd', 'e', 'f']
console.log(removedElements) // ['wrong']
xxxxxxxxxx
//Splice
const numbers = [3, 6, 4, 8, 9, 19, 15, 21, 45, 87];
const numberSplice = numbers.splice(0, 3);
console.log(numberSplice);
//Output: [ 3, 6, 4 ]