Below code demonstrates the usage of splice and slice methods used on Arrays:
// Demonstration of splice function
var Arr1 = [0,1,2,3,4,5];
console.log("Arr1 = ["+Arr1+"]");
var Arr2 = Arr1.splice(2,2); // removes elements 2 and 3
console.log("Arr2 = ["+Arr2+"]");
console.log("Arr1 = ["+Arr1+"]");
// splice removes the elements from the parent array
// and puts it in new array
// Demonstration of slice function
var Arr3 = [0,1,2,3,4,5];
console.log("Arr1 = ["+Arr3+"]");
var Arr4 = Arr3.slice(2,4); // copies elements 2 and 3
console.log("Arr4 = ["+Arr4+"]");
console.log("Arr3 = ["+Arr3+"]");
// slice copies the elements from the parent array
// and puts it in new array
// Demonstration of splice function
var Arr1 = [0,1,2,3,4,5];
console.log("Arr1 = ["+Arr1+"]");
var Arr2 = Arr1.splice(2,2); // removes elements 2 and 3
console.log("Arr2 = ["+Arr2+"]");
console.log("Arr1 = ["+Arr1+"]");
// splice removes the elements from the parent array
// and puts it in new array
// Demonstration of slice function
var Arr3 = [0,1,2,3,4,5];
console.log("Arr1 = ["+Arr3+"]");
var Arr4 = Arr3.slice(2,4); // copies elements 2 and 3
console.log("Arr4 = ["+Arr4+"]");
console.log("Arr3 = ["+Arr3+"]");
// slice copies the elements from the parent array
// and puts it in new array
Refer:
slice - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice
splice - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice
Practice at: http://jsbin.com
No comments:
Post a Comment