what is splice code example

Example 1: splice javascript

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']

Example 2: splice javascript

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"]

Example 3: splice()

/"removes any number of consecutive elements from anywhere in an array."/
let array = ['today', 'was', 'not', 'so', 'great'];

array.splice(2, 2);
// remove 2 elements beginning with the 3rd element
// array now equals ['today', 'was', 'great']

Example 4: splice javascript

let arrDeletedItems = arr.splice(start[, deleteCount[, item1[, item2[, ...]]]])

Example 5: splice in javascript

Splice and Slice both are Javascript Array functions. Splice vs Slice. The splice() method returns the removed item(s) in an array and slice() method returns the selected element(s) in an array, as a new array object. The splice() method changes the original array and slice() method doesn't change the original array.27-Jan-2018

Example 6: what is splice method in javascript

Check Out my CODE-PEN for this Solution 

https://codepen.io/siddhyaOP/pen/eYgRGOe
https://codepen.io/siddhyaOP/pen/eYgRGOe
https://codepen.io/siddhyaOP/pen/eYgRGOe

Tags:

Java Example