what does splice return array javascript code example
Example 1: javascript splice
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: array.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: JavaScript Array Methods .splice()
// elem törlése
let myArray = ['a', 'b', 'c', 'd']
// 0. indextől számolva 2 elemet távolít el
myArray.splice(0, 2)
console.log(myArray)
// --> [ 'c', 'd' ]
// beszúrás
const months = ['Jan', 'March', 'April', 'June'];
// az 1-es indexű helyre illeszt be:
months.splice(1, 0, 'Feb');
console.log(months);
// --> ['Jan', 'Feb', 'March', 'April', 'June']